user3706927
user3706927

Reputation: 69

Implode doesn't work in laravel based on variable data

When I want to implode I use $occu = implode(',',$_POST["occupation"]) and it implodes and the result is 1,2,4.

Now when I use $occu in query like below:

$total = DB::table('store')
->WhereIn('occupation_id',[$occu])
->get();

Then it only fetch the id 1 and not the 2 and 4.

But, if I use in this way:

$total = DB::table('store')
->WhereIn('occupation_id',[1,2,4])
->get();

Then it fetches all three ids.

Therefore, I want to know that why variable based implode first id is taken and not the other two.

Upvotes: 0

Views: 214

Answers (1)

Lakhwinder Singh
Lakhwinder Singh

Reputation: 5582

In Laravel, we need to provide array of values for whereIn function. But you are providing string with comma separated.

You should use this instead

$arrVal = $_POST['ids'];
$items = DB::table('store')
                       ->whereIn('field', $arrVal)
                       ->get();

Above is the example code. Please try this.

Upvotes: 1

Related Questions