I’m using condition based queries but i have a problem how can i use (when) with other table/model because i have to put condition based on the other table column (branch_id).
branch_id comes from steps table.
JavaScript
x
ContactBoard::with(['steps'])
->when($request->has('branch'), function ($query) use ($request) {
$query->where('steps.branch_id', $request->query('branch'));
})
->where('id', $board_id)->get()->first();
Advertisement
Answer
You can make it like
JavaScript
$query->when($request->has('branch'), function ($q)use($request) {
return $q->with('steps',function($q){
$q->where('branch_id', $request->query('branch'));
});
});
$query = $query->get();