Reputation: 597
Got an issue that I am struggling to solve in Laravel 5.
So essentially what I am trying to do is call all the roles a user has into a form that relates to editing a user. Part of that is to loop through the roles first so it displays them all with a checkbox.
Then it should populate all the users roles, i.e. if the user has a role of admin and VIP then those boxes should be ticked. If additional roles are checked or indeed unchecked it needs to then pass thoses changes back to the controller for updating.
So what I have is a User - Roles_User - Roles models and associated tables. A pivot is setup as one (User) to Many (Roles). All of that is working just fine.
I call the edit user form within the controller using the following:
public function edit($id)
{
//
$user = User::with('roles')->findOrFail($id);
return view('admin.users.edit')->with('user', $user);
}
Without boring you with the entire form, all of which works fine except this part which I have been fiddling with.
<div class="form-group">
@foreach (App\Http\Models\Role::all() as $role)
{!! Form::Label('name', $role->name) !!}
{!! Form::checkbox('role[]', $role->id, @if($user->hasRole($role
->id) true @endif, ['class' => 'form-control']) !!}
@endforeach
</div>
This will indeed return all the roles in the table with a checkbox for each.
I have set the ID to the role->id to ensure I can carry that back to the controller. the offending element I am struggling with is setting the value according to the users current roles, which is returning an error.
@if($user->hasRole($role->id) true @endif
In my User model I have the "hasRole" method like so in order to find out if the user has the role.
public function hasRole($id)
{
foreach ($this->roles as $role)
{
if ($role->id == $id) return true;
}
return false;
}
I am not sure if there is something very obvious I am missing here or if I am looking at it from the completely wrong way. I have yet to setup the sync of roles, at this stage I am simply trying to get the roles to populate properly and in such a way I can then update the user record.
Thanks for any assistance.
Solution
Ok, so here is what I should have entered.
{!! Form::checkbox('userrole[]', $role->id, $user->hasRole($role->id),
['class' => 'field']) !!}
This created an array for the user roles with userrole[] and the foreach went in and checked whether the user hasRole and set the check box to true.
Remember that the way this is setup that when I call $user->hasRole it access the hasRole method within the User class.
The $role->id ensures the the array knows which role in the roles table we are referring to.
The user editing the record can then check additional or uncheck the checked roles and the array will carry those changes through when submitted. As the array will have both the checked role id's and of course know the user id.
When returning the array of user roles after the user adds or removes roles I then use the Laravel sync option within the usersController like so:
$user->roles()->sync(Input::get('userrole'));
This will will sync that users roles with the array userroles[] which it gets from the Input when the form was submitted.
Upvotes: 1
Views: 2079
Reputation: 5443
In Html
<input class="field" name="agree" type="checkbox" value="yes">
In laravel get or post method
dd(Input::get('agree'));
Upvotes: 1
Reputation: 77
It is setting the value according to user's current role cause 2nd parameter of checkbox is of 'value'
{!! Form::checkbox('agree', yes, null, ['class' => 'field']) !!}
which will output as;
<input class="field" name="agree" type="checkbox" value="yes">
third parameter can be 'true' which will make checkbox, checked by default.
Upvotes: 1