0

I have a list of ids I want to use to get items out of my database table.

    $permittedClientIds = UsersClientsBridge::where('users_id', auth()->user()->id)->pluck('clients_id')->all();
    $firstPermittedClientIds = UsersClientsBridge::where('users_id', auth()->user()->id)->pluck('clients_id')->first();

    $clientsQuery = Clients::where('id', $firstPermittedClientIds);

    foreach($permittedClientIds as $clientId) {
       $clientsQuery->orWhere('id',$clientId);
    }

    $clientsQuery->get();

    return view('client.clients', [
        'clients' => $clientsQuery
    ]);

this $permittedClientIds contains 10 and 48. When I manually write the orWhere query it returns the expected output but this is retuning 0 results.

I'm not sure if I need $firstPermittedClientIds I only use it so I can initialize my query.

1

1 Answer 1

1

Solved using whereIn

$permittedClientIds = UsersClientsBridge::where('users_id', auth()->user()->id)->pluck('clients_id')->all();
$clientsQuery = Clients::whereIn('id', $permittedClientIds)->get();
return view('client.clients', [
    'clients' => $clientsQuery
]);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.