【问题标题】:How can I select data write where in json object in related ORM Laravel如何在相关 ORM Laravel 的 json 对象中选择数据写入位置
【发布时间】:2019-04-25 20:15:20
【问题描述】:

我有 2 个模型 篮子和产品 我的产品表有 json 对象列,我写这样的代码

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

但是 laravel 不返回数据给我。 抛出这个错误

SQLSTATE[42S22]:未找到列:1054 中的未知列“产品” 'where 子句' (SQL: select * from shopping_cards where json_contains(product->'$.\"store\".\"id\"', 39))

我的数据结构是这样的

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

【问题讨论】:

    标签: php mysql laravel-5 orm eloquent


    【解决方案1】:

    当你在 Laravel Eloquent 中使用 with 时,它不会抓取另一个表,它会创建 2 个查询,所以你不能这样查询。

    你可以使用:

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

    或者使用JOIN语句。

    【讨论】:

      猜你喜欢
      • 2011-01-11
      • 2020-08-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-07-14
      • 1970-01-01
      相关资源
      最近更新 更多