Reputation: 187
I want make form edit user. For the present moment I can edit the data from the user table, but I would like to assign power to the user roles, from the table roles (dropdownlist).
Controller
public function index()
{
$users = User::with('roles')->get();
return view('pages.user', compact('users'));
}
public function update($id, Request $request)
{
$user = User::with('roles')->findOrFail($id);
$user->update($request->all());
return redirect('users');
}
Form
{!! Form::model($user, ['method' => 'PATCH', 'action'=>['UsersController@update', $user->id]]) !!}
<div class="form-group">
<div class="form-group">
{!! Form::label('name','Name: ') !!}
{!! Form::text('name', null, ['class'=>'form-control','placeholder'=>'Here, user name']) !!}
</div>
<div class="form-group">
{!! Form::label('roles','Roles: ') !!}
{!! Form::select('roles',['class'=>'form-control']) !!}
{!! Form::select('roles',$user,null,['class'=>'form-control']) !!}
//i try this but still not working
</div>
</div>
Thx for help.
Upvotes: 0
Views: 1282
Reputation: 21691
I think you want display all role into 'Role' droppdown, check below change of code: Controller
public function index()
{
//Get user detail
$users = User::with('roles')->get();
//Get all roles
$userRole = Roles::lists('name','id');
return view('pages.user', compact('users', 'userRole'));
}
Form
<div class="form-group">
{!! Form::label('roles','Roles: ') !!}
{!! Form::select('roles',$userRole, null,['class'=>'form-control']) !!}<!-- replace with $users['role_id'], if want to display selected role-->
</div>
NOTE: use pluck instead of list for laravel >= 5.3. The lists method on the Collection, query builder and Eloquent query builder objects has been renamed to pluck. The method signature remains the same.
Let me know if still not working!
Upvotes: 1