Skip to content
Advertisement

Laravel find rows by nested relationship column

I have 2 tables reserves and chats, chats belongTo reserves and I want to get all chats where chat.reserve.user_id = Auth::id(). How can I do that in laravel.

Advertisement

Answer

I don`t know your models name (need more code in question), but try use

$chats = Chat::query()->whereHas('reserves', function ($q) use ($authID){
       $q->where('user_id', $authID);       
 })->get();

where ‘reserves’ your relationship and have field user_id. Maybe needed full path to field like ‘reserves’.’user_id’

Also look at documentation

User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement