有条件的Laravel雄辩关系

问题描述 投票:0回答:1

我有一张桌子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获取相关的表值。

php mysql laravel eloquent laravel-eloquent
1个回答
0
投票

使用Use($note)不必要,删除它并使用$q代替

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

如果你想得到所有相关的->with('tasks'),还包括tasks

© www.soinside.com 2019 - 2024. All rights reserved.