Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

shrooq's avatar

Laravel Search in multiple tables

I have 3 SQL tables.

clients events client_events

Because a client can have multiple events, I made the third table to show those relationships. I am using the following code to retrieve all of the clients that have the have a record matching this event, but it is only returning 1 record when there are multiple.

$eventHosts = DB::table('clients')->whereIn('id', function($query) {
    $query->select('client_id')->from('client_events')->where('event_id', '=', explode('/', $_SERVER['REQUEST_URI'])[2]);
})->get();

What am I overlooking?

1 like
2 replies
tisuchi's avatar
tisuchi
Best Answer
Level 70

@shrooq You can try this-

$clientIds = DB::table('client_events')
  ->where('event_id', explode('/', $_SERVER['REQUEST_URI'])[2])
  ->pluck('client_id')
  ->toArray();

$eventHosts = DB::table('clients')->whereIn('id', $clientIds)->get();
6 likes
shrooq's avatar

Thank you. It work now.

2 likes

Please or to participate in this conversation.