1
$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?

NurlanXp
  • 156
  • 2
  • 14
  • 2
    If `$dean_ids` return string like `9,11` then instead of making array like `[$dean_ids]` explode the string using comma. Use something like: `$subjects = Subject::whereIn('dean_id', explode(',', $dean_ids))->select('id')->get(); ` – Sajeeb Ahamed May 24 '20 at 19:32
  • realy good idea it worked, pls answer below i will mark it. – NurlanXp May 24 '20 at 19:37

1 Answers1

2

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();
Sajeeb Ahamed
  • 6,070
  • 2
  • 21
  • 30