0

I want to select the old value of the particular user role,

Roles: administrator, manager, teamleader, agent

blade.php

<div class="form-group">
      <label for="roles">{{ trans('cruds.user.fields.roles') }}*
          <span class="btn btn-info btn-xs select-all">{{ trans('global.select_all') }}</span>
          <span class="btn btn-info btn-xs deselect-all">{{ trans('global.deselect_all') }}</span></label>
      <select name="roles[]" id="roles" class="form-control select2" multiple="multiple" required>
          @foreach($roles as $id => $roles)
              <option value="{{ $id }}" {{ (in_array($id, old('roles', [])) || isset($user) && $user->roles->contains($id)) ? 'selected' : '' }}>{{ $roles }}</option>
           @endforeach
      </select>
</div>

UsersController.php

public function edit(User $user)
{
        $roles = Role::get()->pluck('name', 'name');

        return view('admin.users.edit', compact('user', 'roles'));
}

 public function update(UpdateUsersRequest $request, User $user)
{
   $user->update($request->all());
   $roles = $request->input('roles') ? $request->input('roles') : [];
   $user->syncRoles($roles);

   return redirect()->route('users.index');
}
0
<div class="form-group">
      <label for="roles">{{ trans('cruds.user.fields.roles') }}*
          <span class="btn btn-info btn-xs select-all">{{ trans('global.select_all') }}</span>
          <span class="btn btn-info btn-xs deselect-all">{{ trans('global.deselect_all') }}</span></label>
      <select name="roles[]" id="roles" class="form-control select2" multiple="multiple" required>
          @foreach($roles as $index => $role)
            <option value="{{$role->id}}" @if (request("roles")){{ (in_array($role->id, request("roles")) ? "selected":"") }}@endif>{{$role->role_name}}</option>
           @endforeach
      </select>
</div>

  • Now working Facade\Ignition\Exceptions\ViewException Trying to get property 'id' of non-object – itxrahulsingh Nov 8 at 11:34
  • @itxrahulsingh Show your data roles – Sok Chanty Nov 8 at 11:35
  • updated in question – itxrahulsingh Nov 8 at 11:36
  • @itxrahulsingh seem you don't know about your data from controller clearly. – Sok Chanty Nov 8 at 11:37
  • why alias $roles is your role id? – Sok Chanty Nov 8 at 11:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.