Search code examples
phpsqldatabaselaravelwhere-in

Laravel: whereIn with variable


I'm trying to collect all the records that belong to the sections that happen to them in the variable $sections. But only those from section 1 pick me up. Any suggestions?

$sections = '1,2,3';

$data = News::where('active', '1')
        ->whereIn('section_id', [$sections])
        ->get();

If I substitute $sections in the query for the values, this works, but if I use the variable $sections, It doesn't work.

Thanks.


Solution

  • When you use whereIn() you must pass an array and not string:

    $sections = explode(',', '1,2,3');