4

I'm setting up entrust and trying to manage roles via an edit users page. I have pulled the list of available roles as well as the roles that the user is already assigned to and have them passed to the view as $roles and $assignedRoles.

The problem I'm having is getting the form to show the already assigned roles ($assignedRoles) as checked and the un-assigned roles (remainder of $roles) as not checked.

$roles looks like

    [{"id":1,"name":"Admin","created_at":"2014-09-15 14:26:24","updated_at":"2014-09-15 14:26:24"},{"id":2,"name":"Pastor","created_at":"2014-09-15 14:26:34","updated_at":"2014-09-15 14:26:34"},{"id":3,"name":"Elder","created_at":"2014-09-15 14:26:43","updated_at":"2014-09-15 14:26:43"},{"id":4,"name":"Ministry Leader","created_at":"2014-09-15 14:26:55","updated_at":"2014-09-15 14:26:55"}]

and $userRoles looks like

    [{"id":1,"name":"Admin","created_at":"2014-09-15 14:26:24","updated_at":"2014-09-15 14:26:24","pivot":{"user_id":1,"role_id":1}}]

I am iterating through the available roles in the view using

@foreach ($roles as $role)
    {{ Form::checkbox('role[]', $role->id) }}
    {{ Form::label('role', $role->name) }}<br>
@endforeach

My problem is that I do not know how I can make it so that when viewing the form it will show which roles are already set as per $userRoles (above).

3 Answers 3

6

First you make an array with all data

<?php 
$all_data = array();
foreach($roles as $role){
     $all_data[] =  $role->id;
}
?>

Now you create checkbox

@foreach ($roles as $role)
    {{ Form::checkbox('role[]', $role->id, in_array($role->id, $all_data)) }}
    {{ Form::label('role', $role->name) }}<br>
@endforeach
Sign up to request clarification or add additional context in comments.

1 Comment

Had to make a slight ammendment, instead of using $roles in the foreach I used $userRoles and it is working perfectly, thank you so much
3

In Controller

    $user = User::find($id);
    $allRoles = Role::all();
    $assignedRoles  = $user->roles->pluck('id')->toArray();

In View

<div class="form-group row" id="assign_hostel_fees">
   <label for="inputEmail3" class="col-sm-3 control-label">Select Roles</label>
      <div class="col-sm-9">
          @foreach($allRoles as $role)
             <div class="form-check">
                 <label class="form-check-label">
                    <input class="form-check-input" type="checkbox" name="roles[]" value="{{$role->id}}" {{in_array($role->id,$assignedRoles)?'checked':''}}>
                    {{$role->name}}
                 </label>
             </div>
          @endforeach
       </div>
 </div>

Comments

1

this might help you -

{{ Form::checkbox('agree', 1, true) }}

will generate -

<input checked="checked" name="agree" type="checkbox" value="1">

the third parameter defines if it will be checked or not. depending on the data(check the role id of $userRoles with $roles) generate a variable with true or false value.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.