2017-08-01 206 views
2

我想救我的支點,但什麼都沒有發生,它不提示我不知道在哪裏可以調試原因沒有提示,任何想法,任何錯誤?附在這裏是我的代碼。更新數據透視表Laravel 5.4

控制器注意:電子郵件和姓名正確更新。

public function update(Request $request, User $User) 
    { 
     $user=auth()->user(); 

     $User->name    = $request->get('name'); 
     $User->email   = $request->get('email'); 
     $User->warehouse_id  = $request->get('warehouse_id'); 
     $User->role_id   = $request->get('role_id'); 

     $User->save(); 

     $User->roles()->updateExistingPivot($request->get('role_id'), ['role_id' => $request->get('role_id')]); 

     $User->warehouse()->updateExistingPivot($request->get('warehouse_id'), ['warehouse_id' => $request->get('warehouse_id')]); 

     return redirect()->route('user.index'); 
    } 

模型關係

倉庫

class Warehouse extends Model 
{ 
    protected $fillable = ['warehouse','warehouse_description','created_by']; 

    public $primaryKey='id'; 

    public function users() 
    { 
     return $this->belongsToMany(User::class); 
    } 
} 

用戶

public function roles() 
{ 
    return $this->belongsToMany(Role::class); 
} 

public function warehouse() 
{ 
    return $this->belongsToMany(Warehouse::class); 
} 

角色

class Role extends Model 
{ 
    protected $fillable = ['role']; 

    public $primaryKey='id'; 

    public function users() 
    { 
     return $this->belongsToMany(User::class); 
    } 
} 

回答

1

我所做的是把yusing updateExistingPivot

這裏之前isnerting預覽ID是我的控制器

公共功能更新(請求$請求,用戶$ USER) {

$User->name    = $request->get('name'); 
    $User->email   = $request->get('email'); 
    $User->warehouse_id  = $request->get('warehouse_id'); 
    $User->role_id   = $request->get('role_id'); 

    $User->roles()->updateExistingPivot($User->role_id, ['role_id' => $request->get('role_id')]); 

    $User->warehouse()->updateExistingPivot($User->warehouse_id, ['warehouse_id' => $request->get('warehouse_id')]); 

    $User->save(); 

    return redirect()->route('user.index'); 
}