Dax
Dax

Reputation: 827

Getting multiple input fields in POST request

What am I building: I'm trying to build an application that allows users to predict the scores of the upcoming soccer games, for each game there are 2 input fields, one for the homeTeam and one for the awayTeam.

Goal: After the user predicted the score I want to retrieve all the number input fields + the name of the homeTeam and awayTeam. I need some way of knowing that those 2 number input fields represent the correct match. (I have a match-id)

With following code I only receive the last input field, does laravel provide any methods to get every input field? + how can I add the name of the teams to my request? (they aren't input fields)

Code:

<ul class="match-table">
    {!! Form::open(['method'=>'POST', 'action'=>'PredictionController@store']) !!}
            @foreach($matches as $match)
                <div class="specific-match-table container mb-5">
                   <div class="row">
                        <div class="team d-flex align-items-center col-sm-5">
                            <p>{{$match->homeTeam}}</p>
                        </div>
                        <div class="row col-sm red">
                            {{Form::number('homeTeam', '', ['placeholder' => '0', 'class' =>'form-control col-sm'])}}
                            <span class="vs">vs</span>
                            {{Form::number('awayTeam', '', ['placeholder' => '0', 'class' =>'form-control col-sm'])}}
                        </div>
                        <div class="team d-flex align-items-center justify-content-end col-sm-5">
                            <p>{{$match->awayTeam}}</p>
                        </div>
                    </div>
                </div>
            @endforeach
     {{Form::button('Submit', ['type' =>'submit', 'class' => 'submit-btn'])}}              
     {!! Form::close() !!}
</ul>

Controller:

class PredictionController extends Controller
{
  public function store(Request $request) {

      $requestData = $request->all();

      dd($requestData);
  }
}

DD result:

array:3 [▼
  "_token" => "4nhqdD7rEfl4BmDyT6KiWn5zAD4r2EHaKIvXJQmJ"
  "homeTeam" => "5"
  "awayTeam" => null
]

"5" => is the number on the last input field

Upvotes: 0

Views: 852

Answers (2)

Marwelln
Marwelln

Reputation: 29413

What you want is to use arrays.

Change Form::number('homeTeam', to Form::number('match[' . $match->id . '][homeTeam]', and Form::number('awayTeam', to Form::number('match[' . $match->id . '][awayTeam]',.

You will then get a result as $match[N][awayTeam] = X. Run dd($request->get('match')) in your controller to view the array.

If you also want the name of each team use a hidden input field inside your loop.

<input type='hidden' name='match[{{ $match->id }}][awayTeamName]' value='{{ $match->awayTeam }}' />
<input type='hidden' name='match[{{ $match->id }}][homeTeamName]' value='{{ $match->homeTeam }}' />

Upvotes: 1

lawra
lawra

Reputation: 147

Add '[]' to the name of your fields, this will make an array of values. Like 'homeTeam[]' instead of 'homeTeam'. So you will get for example:

'homeTeam' => array(
"1",
"3",
"5"
)

Upvotes: 1

Related Questions