我正在尝试将默认值绑定到选择标记.(在"编辑视图"中).
我知道这应该很容易,但我想我错过了一些东西.
我有:
User.php(我的用户模型)
...
public function groups()
{
return $this->belongsToMany('App\Group');
}
public function getGroupListAttribute()
{
return $this->groups->lists('id');
}
...
Run Code Online (Sandbox Code Playgroud)
UserController.php(我的控制器)
...
public function edit(User $user)
{
$groups = Group::lists('name', 'id');
return view('users.admin.edit', compact('user', 'groups'));
}
...
Run Code Online (Sandbox Code Playgroud)
edit.blade.php(视图)
...
{!! Form::model($user, ['method' => 'PATCH', 'action' => ['UserController@update', $user->id]]) !!}
...
...
// the form should be binded by the attribute 'group_list' created
// at the second block of 'User.php'
// performing a $user->group_list gets me the correct …
Run Code Online (Sandbox Code Playgroud)