【问题标题】:Eloquent relationship - whereNotNull does not exist雄辩的关系 - whereNotNull 不存在
【发布时间】:2018-12-05 17:55:17
【问题描述】:

我正在尝试查询 Eloquent 中的关系。

$deliveryOverride = $product->days->whereNotNull('margin')
->where('price_id', $order['price_id'])
->where('product_id', $product->id)
->where('sale_at', date('Y-m-d', strtotime($day)))
->first();

我不断收到错误

NotNull 不存在的方法。

有什么想法吗?

【问题讨论】:

    标签: laravel eloquent


    【解决方案1】:

    当您直接在 Eloquent 对象上调用关系属性时,会执行查询并返回 Collection。集合上没有WhereNotNull 函数。

    如果要使用该函数查询关系,则必须直接调用关系函数。由于查询将在数据库上进行,因此这也将提高性能。

    $deliveryOverride = $product->days()  // Call relation function here
        ->whereNotNull('margin')
        ->where('price_id', $order['price_id'])
        ->where('product_id', $product->id)
        ->where('sale_at', date('Y-m-d', strtotime($day)))
        ->first();
    

    可以在此处的文档中找到更多信息:https://laravel.com/docs/5.7/eloquent-relationships#querying-relations

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-04-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-12-15
      相关资源
      最近更新 更多