2017-02-27 60 views
0

我有这个类如何观察Laravel 5中的外键?

class User extends Model { 
     public function roles() { 
      return $this->belongsToMany('App\Models\SystemRole', 'app_system_role_users', 'user_id', 'role_id', 'id'); 
} 
} 

app_system_role_users包含地图从user_idrole_id

我想保存全部对此表的更改。

我该如何观察外交关系?可能会发生一些事件吗?

+1

你应该clarifiy你所说的 '观察的关系' 是什么意思 – zedling

回答

0
use Illuminate\Database\Eloquent\Relations\Pivot; 

$user->roles()->newExistingPivot()->deleted(function (Pivot $pivot) { 
    // 
}); 

​​