0

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.

2
  • In our second example its array but in you 1st example its string even if u put square brackets around it. For example ["1,2,3"] this is what ur getting with a implode and what are you passing in second example [1,2,3] see the difference. Commented Nov 25, 2018 at 7:22
  • If you are already getting array don't implode it just pass it without implode. It will work. Commented Nov 25, 2018 at 7:24

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.