我有一张 table note
与 id, 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 仅获取相关表值。
最佳答案
不必要使用 Use($note)
,删除它并使用 $q
代替
$note = Notes::where('deleted_at', null)
->whereHas('tasks', function($q){
$q->where('type_id',601);
})->get();
如果您想获取所有相关的任务
,还请包括->with('tasks')
关于php - 条件 Laravel Eloquent 关系,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47898860/