【问题标题】:How to put where in relation to method?如何放置与方法相关的位置?
【发布时间】:2020-12-10 07:20:30
【问题描述】:

下面是我的 Eloquent 查询。

$cat = Category::with('subcategory.items.products')
    ->where('id',$discates)->first();

我想用status = 1.在产品上添加位置

where('status',1)

【问题讨论】:

    标签: php laravel eloquent laravel-8


    【解决方案1】:

    你应该花一些时间阅读 Laravel 文档。它们充满了示例和非常好的基础学习资源。

    https://laravel.com/docs/8.x/eloquent-relationships#constraining-eager-loads

     $cat = Category::with(['subcategory.items.products' => function($query){
        $query->where('status', 1);
    
    }])->where('id',$discates)->first();
    

    【讨论】:

      【解决方案2】:

      试试这个

      $query = Category::query();
      
      $query->whereHas('products', function ($q) {
          $q->where('status', 1);
      });
      
      
      $cat = $query->with('subcategory.items.products')->find($discates); 
      

      $cat = $query->with('subcategory.items.products')>where('id',$discates)->first();
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-04-01
        • 1970-01-01
        • 1970-01-01
        • 2011-02-18
        相关资源
        最近更新 更多