Skip to content
Advertisement

Laravel WhereIn array returns only first index results

$dean_ids = Auth::user()->dean_id; // "9,11"
$subjects = Subject::whereIn('dean_id', [$dean_ids])->select('id')->get();

returns only data for “9” but when i trying like this:

$subjects = Subject::whereIn('dean_id', [9,11])->select('id')->get(); 
//it returns all data that what i want.

how can i fix it?

Advertisement

Answer

As I see, this line $dean_ids = Auth::user()->dean_id; returns a comma-separated string. So when you make $dean_ids array by using [$dean_ids] it actually makes an array like:

array(
  '9,11'
)

Instead of

array(
  9,
  11
)

There is only one value inside the array. So what you can do just use explode for splitting the string by using a comma and it also returns an array.

You can try this:

$subjects = Subject::whereIn('dean_id', explode(',', $dean_ids))->select('id')->get();
User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement