有条件的Laravel雄辩关系

时间:2017-12-20 04:36:52

标签: php mysql laravel eloquent laravel-eloquent

我有noteid, note, type_id, about_id。这里type_id引用了不同类型,如task(601), discussion (602), images (603),等。about_id引用了受尊重类型表的ID。

$note = Notes::with('tasks')->with('discussion')->with('images')->get();

此返回的注释包含所有->with()表值。如何根据type_id获取相关表的值。我试过了

$note=Notes::where('deleted_at', null);
$note=$note->WhereHas('tasks', function($q) use($note)
{
    $note->Where('type_id',601);
})->get();

是否可以使用eloquent获取相关的表值。

1 个答案:

答案 0 :(得分:0)

不必使用Use($note),删除此内容并使用$q代替

   $note = Notes::where('deleted_at', null)
   ->whereHas('tasks', function($q){ 
        $q->where('type_id',601); 
   })->get();

如果您想要获得所有相关的->with('tasks')

,还要包含tasks
相关问题