joenpc npcsolution
joenpc npcsolution

Reputation: 767

How to get selected radio input array in controller on Laravel

I'm working on Laravel with radio input group and trying to pass to controller. Below is my blade.

    @foreach ($testtransactions as $key => $testtransaction)
         <div class="form-check">
           <input type="radio" name="choice{{$key}}" id="1" value="1" class="form-check-input">
         </div>
         <div class="form-check">
           <input type="radio" name="choice{{$key}}" id="2" value="2" class="form-check-input">
         </div>
         <div class="form-check">
           <input type="radio" name="choice{{$key}}" id="3" value="3" class="form-check-input">
         </div>
         <div class="form-check">
           <input type="radio" name="choice{{$key}}" id="4" value="4" class="form-check-input">
         </div>
    @endforeach

I try to get value in controller as below code but not success.

public function TestSave(Request $request){
    $choices = $request->choice;
    return $choices;
}

Any advice or guidance on this would be greatly appreciated, Thanks

Upvotes: 1

Views: 2534

Answers (3)

Pablo
Pablo

Reputation: 6058

This is simple HTML. Assuming $key is unique you need to make the radio button name choice[{{$key}}].

In the backend/controller you access it as $request->choice just keep in mind that it will be an array.

Upvotes: 2

Danny Escalante
Danny Escalante

Reputation: 23

friends, radio invoked

@foreach ($testtransactions as $key => $testtransaction)
         <div class="form-check">
           <input type="radio" name="choice[]" id="1" value="1" class="form-check-input" {{ $testtransaction->active) ? 'checked' : ''}} >
         </div>
         <div class="form-check">
           <input type="radio" name="choice[]" id="2" value="2" class="form-check-input" {{ $testtransaction->active) ? 'checked' : ''}} >
         </div>
         <div class="form-check">
           <input type="radio" name="choice[]" id="3" value="3" class="form-check-input" {{ $testtransaction->active) ? 'checked' : ''}} >
         </div>
         <div class="form-check">
           <input type="radio" name="choice[]" id="4" value="4" class="form-check-input" {{ $testtransaction->active) ? 'checked' : ''}} >
         </div>
    @endforeach

Upvotes: 0

MohamedSabil83
MohamedSabil83

Reputation: 1559

Chang radios name from choice{{$key}} to choice[]

Upvotes: 0

Related Questions