Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/file/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Laravel 5.6中多对多关系中更新、存储和删除的正确方法_Laravel_Laravel 5_Laravel 5.6 - Fatal编程技术网

Laravel 5.6中多对多关系中更新、存储和删除的正确方法

Laravel 5.6中多对多关系中更新、存储和删除的正确方法,laravel,laravel-5,laravel-5.6,Laravel,Laravel 5,Laravel 5.6,我有这段代码,它可以用于存储和更新,但我不知道如何通过detach方法删除用户角色。如何从多个用户的角色中删除单个角色?我不确定roles()->attach()行中的代码,我认为你们有更好的解决方案 用户控制器: public function store(Request $request) { if($request->isMethod('put')) { $user = User::findOrFail($request->id);

我有这段代码,它可以用于存储和更新,但我不知道如何通过
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');
  }
  public function users(){
      return $this->belongsToMany('App\User');
  }
Role.php

  public function roles(){
      return $this->belongsToMany('App\Role');
  }
  public function users(){
      return $this->belongsToMany('App\User');
  }
数据透视表

   Schema::create('role_user', function (Blueprint $table) {
        $table->increments('id');
        $table->integer('user_id')->unsigned();
        $table->integer('role_id')->unsigned();
   });
一种方法(不是说这是正确的方法)是使用该方法(同步关联

我在表单中创建了一个下拉列表。
我在一个模型中有一个函数,它执行以下操作:

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);

谢谢你的详细回答,我还通过更改附加到同步来工作,这是你建议的。