如何在相关orm laravel中选择data write where in json对象

z9ju0rcb  于 2021-06-17  发布在  Mysql
关注(0)|答案(1)|浏览(265)

我有两个模型篮和产品我的产品表有json对象列,我写的代码如下

$basket=Basket::with("product")->whereJsonContains("product->store->id",$stores_id)->get();

但拉威尔不给我回数据。抛出此错误
sqlstate[42s22]:找不到列:“where子句”中的1054未知列“product”(sql:select*from) shopping_cards 其中json包含( product ->“$.\”存储\“\”id \“',39))
我的数据结构是这样的

{
        "id": 3,
        "users_id": 1,
        "quantity": 1,
        "product": {
            "id": 116,
            "store": {
                "id": 39,
                "status": 41,
            }
         }
}
thigvfpy

thigvfpy1#

当你使用 with 在laraveleleloquent中,它不会抓取另一个表,而是创建两个查询,因此不能这样查询。
您可以使用:

$basket = Basket::whereHas('product', function ($query) use ($stores_id) {
    $query->whereJsonContains('store->id', $stores_id);
})->get();

或者使用join语句。

相关问题