简体   繁体   中英

Laravel 5 Form Model Binding Radiobutton

Every user is supposed to have a role (only one). For that purpose I am using Spaties laravel-permission package.

I'm using a bunch of radio buttons in my edit and create user forms, where you are supposed to be able to select a role for the user. I got it to work with checkboxes, but at the moment you can select multiple checkboxes und thus add multiple roles to one user. Now I tried to use radiobuttons, it works, but does not pre-select the buttons.

Here is my checkbox code:

      @foreach ($roles as $role)
          @role('administrator')
              {!! Form::checkbox('roles[]', $role->id) !!}
              {!! Form::label($role->name, ucfirst($role->name)) !!}<br>
          @endrole
          @role('manager')
            @if(!($role->name == 'administrator'))
              {!! Form::checkbox('roles[]', $role->id) !!}
              {!! Form::label($role->name, ucfirst($role->name)) !!}<br>
            @endif
          @endrole
      @endforeach 

And here my radio button code:

      @foreach ($roles as $role)
          @role('administrator')
              {!! Form::radio('roles[]', $role->id) !!}
              {!! Form::label($role->name, ucfirst($role->name)) !!}<br>
          @endrole
          @role('manager')
            @if(!($role->name == 'administrator'))
              {!! Form::radio('roles[]', $role->id) !!}
              {!! Form::label($role->name, ucfirst($role->name)) !!}<br>
            @endif
          @endrole
      @endforeach

You have to pass a third parameter to the function So try:

{!! Form::radio('roles[]', $role->id, TRUE) !!}

Or you can make an inline if statement instead if you need to calculate something first

Try

{!! Form::radio('role', $role->id, isset($employee) ? $employee->role_id === $role->id : false) !!}

If you have the user, check their role id and if its the same as the one on the loop, marked as selected

So, little update:

I used the array in my controller to associate users with roles, as I was able to select multiple roles before. Now I changed my controller to only take the one selected role and associate it with the user and that works.

Code:

      @foreach ($roles as $role)
          @role('administrator')
              {!! Form::radio('role', $role->id, $employee->hasRole($role)) !!}
              {!! Form::label($role->name, ucfirst($role->name)) !!}<br>
          @endrole
          @role('manager')
            @if(!($role->name == 'administrator'))
              {!! Form::radio('role', $role->id, $employee->hasRole($role)) !!}
              {!! Form::label($role->name, ucfirst($role->name)) !!}<br>
            @endif
          @endrole
      @endforeach

Controller:

      $role = $request->get('role');
      $employee->syncRoles($role);

That works, but isn't the whole idea of form model binding that you don't have to do it manually? If somebody could provide an answer, or a better solution, that would be great.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM