当使用的多个 trait中包含了相同的方法名,将会发生冲突,冲突错误信息如下
fatalerrorexception in user.php line 43: trait method xxxxxx has not been applied, because there are collisions with other trait methods on app\http\models\user
和 softdeletes 的 restore 冲突 由于 entrustusertrait和 softdeletes两个 trait都包含 restore方法,所以当我们对用户 model 使用软删除的时候同时集成 entrust的时候就会导致冲突。
解决方法就是引用两个 trait时为 restore方法设置别名,然后重写一个 restore方法,分别调用两个 restore方法。代码如下:
class user extends model implements authenticatableinterface { use authenticatable; use entrustusertrait { restore as private restorea; } use softdeletes { restore as private restoreb; } /** * 解决 entrustusertrait 和 softdeletes 冲突 */ public function restore() { $this->restorea(); $this->restoreb(); }}
和 authorizable 的 can 冲突 解决办法是将 entrustusertrait的 can方法改一个别名,然后使用 authorizable中的 can,代码如下
use authenticatable, canresetpassword, presentabletrait, authorizable, entrustusertrait { entrustusertrait::can as may; authorizable::can insteadof entrustusertrait;}
参考: laravel 5.1.11 - trait method can has not been applied, because there are collisions with other trait methods on app\user