Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 514
nature Avatar asked Mar 07 '23 02:03

nature


1 Answers

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

$sections = explode(',', '1,2,3');
like image 152
Alexey Mezenin Avatar answered Mar 17 '23 00:03

Alexey Mezenin