0

i'm trying to send an array from my laravel view to my controller, but i'm only receiving one part os the data, this is what i have:

<form
                                    method="POST"
                                    action="{{ url('/forms/reports') }}"
                            >
                            <input type="hidden" name="_token" value="{{ csrf_token() }}">
                            @foreach ($results as $result)
                                       <input value="{{ $result->code }}" name="code"> 
                                       <input value="{{$result->name}}" name="name"> 
                                       <input value="{{$result->user}}" name="user"> 
                                       <input value="{{$result->number}}" name="number"> 
                            @endforeach
                            <div class="col-xs-9">
                            </div>
                            <div class="col-xs-3 container-download">
                                <button type="submit" class="btn btn-download" id="btn-download" >Download</button>
                            </div>
                            
                        </form>

But results has this:

array (
  0 => '1',
  1 => 'Test Name 1',
  2 => 'user1',
  3 => '1',
), array (
0 => '2',
  1 => 'Test Name 2',
  2 => 'user2',
  3 => '2',
);

And on the table that i have on my view is showing correctly, the two rows of data. But when i do the post to receive on my controller the full results array, i only get the second row, when i print it like this:

public function generateExcel(Request $request)
    {
    $code = $request->input('code');
    $name = $request->input('name');
    $user = $request->input('user');
    $number = $request->input('number');

    $users = [$code, $name, $user, $number];

    Log::debug($users);
}

And my Log shows me this:

[2020-12-30 12:43:58] local.DEBUG: array (
  0 => '2',
  1 => 'Test Name 2',
  2 => 'user2',
  3 => '2',
) 

And i don't know if i should push the values first or i'm making another mistake. Can anyone help me with this?

1 Answer 1

1

That is happening because you are not sending an array. You will need to do something like this.

@foreach ($results as $result)
    <input value="{{ $result->code }}" name="code[]"> 
    <input value="{{$result->name}}" name="name[]"> 
    <input value="{{$result->user}}" name="user[]"> 
    <input value="{{$result->number}}" name="number[]"> 
@endforeach
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you! That was the mistake, now is working perfectly.

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.