Reputation: 3704
My Code
$videos = ExamVideo::with([
'user' => function ($query) use ($request) {
$query->where('user_name', 'like', '%'.$request->search.'%');
}
])->where('is_marked','=', 0)->get();
return $videos;
I want to get only those videos where is_marked
is zero & it's relation user_name
match my search result.
But I get all the videos which marked is zero.
Upvotes: 2
Views: 86
Reputation: 14921
The with
method is only for eager loading and not used to filter your records.
You can accomplish what you want with the whereHas
method which will perform a query on the relation:
$videos = ExamVideo::whereHas('user', function ($query) use ($request) {
$query->where('user_name', 'like', "%{$request->search}%");
})->where('is_marked', false)->get();
For more information about whereHas
: https://laravel.com/docs/6.x/eloquent-relationships#querying-relationship-existence
Upvotes: 2