delv123
delv123

Reputation: 146

Send array data from laravel view to controller

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?

Upvotes: 0

Views: 1552

Answers (1)

nicholasnet
nicholasnet

Reputation: 2277

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

Upvotes: 1

Related Questions