0

I have this on my blade file:

{{ Form::open(['route' => 'my_route_name']) }}

  <button type="submit" class="btn btn-sm btn-success">
  <i class="fa fa-file-excel-o" aria-hidden="true"></i> Download
  </button>

  {{ Form::hidden('my_ids', $my_ids) }}

{{ Form::close() }}

Checking on the chrome's developer mode, the value of my hidden textbox named my_ids is:

[1,2,3,4,5,6]

Upon clicking the Download button, it goes on my controller:

$results= Model::whereIn('id', $request->my_ids)->get();

This is where I am getting an error.

DD-ing dd($request->my_ids) on my controller gives me "[1,2,3,4,5,6]".

However, if I just put the values directly on the eloquent query like below, it would work.

$results= Model::whereIn('id', [1,2,3,4,5,6])->get();

Am I missing something here?

3
  • What error does it give you? Commented Jun 9, 2019 at 9:46
  • it says Invalid argument supplied for foreach() then it's highlighting my $results= Model::whereIn('id', $request->my_ids)->get(); query Commented Jun 9, 2019 at 9:50
  • I assume that $request->my_ids gives you an array like string however you can improve your query with Model::findMany($request->my_ids); Commented Jun 9, 2019 at 9:54

1 Answer 1

3

Your dd shows that $request->my_ids is a string, therefore you must parse it before you use it as array.

Try

$results= Model::whereIn('id', json_decode($request->my_ids))->get();
Sign up to request clarification or add additional context in comments.

1 Comment

@kapitan my pleasure! :)

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.