Laravel 5.6 中多对多关系中更新、存储和删除的正确方法
Correct way of updating, storing and deleting in many-to-many relationship in Laravel 5.6
我有这段代码,它有点用于存储和更新,但我不知道如何通过 detach
方法删除用户的角色。如何从多个用户的角色中删除单个角色?我不确定 roles()->attach()
行中的代码,我认为你们对此有更好的解决方案。
用户控制器:
public function store(Request $request)
{
if($request->isMethod('put'))
{
$user = User::findOrFail($request->id);
$user->name = $request->input('name');
$user->email = $request->input('email');
$role = Role::where('name',$request->input('role_id'))->first();
$user->roles()->attach($role->id);
}
else
{
$user = new User;
$user->name = $request->input('name');
$user->email = $request->input('email');
$user->password = Hash::make($request->input('password'));
$user->remember_token = str_random(40);
$user->id = User::count() + 1; //get the last user id? for pivot table
$user->roles()->attach(2); //default role (2) : admin is (1)
}
if($user->save()){
return response()->json($user);
}
}
User.php
public function roles(){
return $this->belongsToMany('App\Role');
}
Role.php
public function users(){
return $this->belongsToMany('App\User');
}
旋转 table
Schema::create('role_user', function (Blueprint $table) {
$table->increments('id');
$table->integer('user_id')->unsigned();
$table->integer('role_id')->unsigned();
});
一个方法(不是说是正确的方法)是使用sync方法(Syncing Associations)。
我在表单中创建了一个下拉列表。
我在模型中有一个函数执行以下操作;
public function selectListValues($key, $value, $where=[])
{
$list = [];
if (is_array($value)) {
array_unshift($value, $key);
$result = $this->model->where($where)->get($value);
foreach ($result->toArray() as $r) {
$index = $r[$key];
unset($r[$key]);
$list[$index] = implode(' ', $r);
}
} else {
try {
$result = $this->model->where($where)->get()->pluck($value, $key);
$list = $result->toArray();
} catch(ModelNotFoundException $e){
return null;
}
}
return $list;
}
我在生成表单的函数中调用了它;
$roles = $roleModel->selectListValues('id', 'name');
然后在我控制器的存储方法中;
$roles = $request->get('roles');
$collection = new Collection();
foreach ($roles as $role) {
try {
$collection->add(Role::where('id', '=', $role)->firstOrFail());
} catch (\Exception $e) {
dd($e);
}
}
if (empty($collection)) {
throw new \Exception('No roles found for '.$user->email);
}
$user->roles()->sync($collection);
我有这段代码,它有点用于存储和更新,但我不知道如何通过 detach
方法删除用户的角色。如何从多个用户的角色中删除单个角色?我不确定 roles()->attach()
行中的代码,我认为你们对此有更好的解决方案。
用户控制器:
public function store(Request $request)
{
if($request->isMethod('put'))
{
$user = User::findOrFail($request->id);
$user->name = $request->input('name');
$user->email = $request->input('email');
$role = Role::where('name',$request->input('role_id'))->first();
$user->roles()->attach($role->id);
}
else
{
$user = new User;
$user->name = $request->input('name');
$user->email = $request->input('email');
$user->password = Hash::make($request->input('password'));
$user->remember_token = str_random(40);
$user->id = User::count() + 1; //get the last user id? for pivot table
$user->roles()->attach(2); //default role (2) : admin is (1)
}
if($user->save()){
return response()->json($user);
}
}
User.php
public function roles(){
return $this->belongsToMany('App\Role');
}
Role.php
public function users(){
return $this->belongsToMany('App\User');
}
旋转 table
Schema::create('role_user', function (Blueprint $table) {
$table->increments('id');
$table->integer('user_id')->unsigned();
$table->integer('role_id')->unsigned();
});
一个方法(不是说是正确的方法)是使用sync方法(Syncing Associations)。
我在表单中创建了一个下拉列表。
我在模型中有一个函数执行以下操作;
public function selectListValues($key, $value, $where=[])
{
$list = [];
if (is_array($value)) {
array_unshift($value, $key);
$result = $this->model->where($where)->get($value);
foreach ($result->toArray() as $r) {
$index = $r[$key];
unset($r[$key]);
$list[$index] = implode(' ', $r);
}
} else {
try {
$result = $this->model->where($where)->get()->pluck($value, $key);
$list = $result->toArray();
} catch(ModelNotFoundException $e){
return null;
}
}
return $list;
}
我在生成表单的函数中调用了它;
$roles = $roleModel->selectListValues('id', 'name');
然后在我控制器的存储方法中;
$roles = $request->get('roles');
$collection = new Collection();
foreach ($roles as $role) {
try {
$collection->add(Role::where('id', '=', $role)->firstOrFail());
} catch (\Exception $e) {
dd($e);
}
}
if (empty($collection)) {
throw new \Exception('No roles found for '.$user->email);
}
$user->roles()->sync($collection);