How to use '::whereHas()' as a constraint for '::with()' query when using Eloquent?

后端 未结 1 1549
故里飘歌
故里飘歌 2021-01-23 14:52

I have a query:

Posts::whereHas(\'comments\', function($query){
          $query->where(\'name\', \'like\', \'asd\');
      })->with(\'comments\')->get         


        
相关标签:
1条回答
  • 2021-01-23 15:26

    You can create query something like this, almost without duplicate :)

    $callback = function($query) {
         $query->where('name', 'like', 'asd');
    }
    Posts::whereHas('comments', $callback)->with(['comments' => $callback])->get();
    

    Or, you can make like in this post Laravel - is there a way to combine whereHas and with

    0 讨论(0)
提交回复
热议问题