Search code examples
phplaraveleloquentlaravel-query-builder

Laravel Eloquent, get all datas in ids array with whereIn?


Here my code :

$search = request()->get('search');
    $conciergerieSelect = request()->get('conciergerie');

    $services = Service::get();

    $prestations = Prestation::with([
        'service:name'
    ])
    ->whereIn('conciergerie_ids', $conciergerieSelect)
    ->where('name', 'regexp', "/$search/i")
    ->paginate(100);

    return $res = [
        'prestations' => $prestations,
        'services' => $services
    ];

I need to get all prestations where there is an conciergerie_ids equal to $conciergerieSelect.

conciergerie_ids is a table of ids.

$conciergerieSelect is an id.

I tried to use whereIn but i get an error : "Invalid supplied foreach()" Thank you.


Solution

  • The whereIn method provided by Laravel accepts an array as the 2nd parameter.

    Please replace the $conciergerieSelect variable by [$conciergerieSelect] in the whereIn clause:

    //...
    ->whereIn('conciergerie_ids', [$conciergerieSelect])