4

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.

Nimeshka Srimal
  • 8,012
  • 5
  • 42
  • 57
nature
  • 307
  • 5
  • 23

2 Answers2

7

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

$sections = explode(',', '1,2,3');
Alexey Mezenin
  • 158,981
  • 26
  • 290
  • 279
0

Your $sections variable is a string. And you are passing that string to the array, not each individual numbers in the string.

Ex:

$users = DB::table('users')
                ->whereIn('id', [1, 2, 3])
                ->get();
Nimeshka Srimal
  • 8,012
  • 5
  • 42
  • 57