HammockKing
HammockKing

Reputation: 77

Laravel: forms and radiobuttons

I am just wondering if i could ask a fairly simple question about radio buttons and Laravel as the googlebox isnt co-operating. My code is thus:

{{ Form::open(array('url'=>'users/create', 'method' => 'post')) }}
    <tbody>
    <label><span>&nbsp;</span>{{ Form::submit('Approve/Decline')}}</label>
    @foreach($users as $user)
        <tr>
            <td>{{{$user->firstname}}}</a></td>
            <td>{{{$user->secondname}}}</a></td>
            <td>{{{$user->address}}}</a></td>
            <td>{{{$user->city}}}</a></td>
            <td>{{{$user->phone}}}</a></td>
            <td>{{{$user->id}}}</td>
            <td>{{{$user->username}}}</a></td>
            <td>{{{$user->email}}}</tds>
            <td>{{{$user->type}}}</a></td>
            <td>{{Form::radio('Membership[]', 'approve')}}</a></td>
            <td>{{Form::radio('Membership[]', 'decline')}}</a></td>

        </tr>
    @endforeach
    </tbody>


{{ Form::close() }}

A simple form with my two text boxes at the end there. This turns on fine in the browser but once i check one radio button the others deselect which would be fine in this order:

O O

but since its in a loop, there are many rows of O O and i can only choose one out of all of the different rows. I would imagine I would need to group each two together somehow dynamically but I'm unsure. All aid appreciated.

Upvotes: 0

Views: 930

Answers (1)

Antonio Carlos Ribeiro
Antonio Carlos Ribeiro

Reputation: 87719

Probably you'll need to set a different name for each of your checkboxes/radio buttons:

<td>{{Form::radio("Membership[".$user->id."]", 'approve')}}</a></td>
<td>{{Form::radio("Membership[".$user->id."]", 'decline')}}</a></td>

To get your values in your controller you just have to:

dd( Input::get('Membership')[$user->id] );

In return you should get

approve

or

decline   

Note that for this to work this way I removed the quotes from

"Membership[".$user->id."]"

Here are two routes I used to test it:

The form:

Route::any('test', function() {
    return Form::open(array('url' => 'radio', 'method' => 'post')) .
    Form::radio("Membership[1]", 'approve') .
    Form::radio("Membership[1]", 'decline') .
    Form::submit('send') .
    Form::close();
});

The result:

Route::any('radio', function() {
    dd( Input::get('Membership')[1] );
});

Upvotes: 1

Related Questions