Solve the Trait conflict of Entrust because a friend was asking me [here], and I happened to have encountered it before, so I recorded it.
If multiple trait nodes contain the same method name, a conflict occurs. the conflict error message is as follows:
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
Conflicts with the restore of SoftDeletes
Because both EntrustUserTrait and SoftDeletes trait contain the restore method, when we use soft delete for the user Model and integrate the Entrust, a conflict may occur.
The solution is to set an alias for the restore method when two trait is referenced, and then override a restore method to call two restore methods respectively. The code is as follows:
Class User extends Model implements AuthenticatableInterface {use Authenticatable; use EntrustUserTrait {restore as private restoreA;} use SoftDeletes {restore as private restoreB ;} /*** resolve the conflict between EntrustUserTrait and SoftDeletes */public function restore () {$ this-> restoreA (); $ this-> restoreB ();}}
Can conflict with Authorizable
The solution is to change the can Method of EntrustUserTrait to an alias, and then use the can Method in Authorizable. the code is as follows:
use Authenticatable, CanResetPassword, PresentableTrait, Authorizable, EntrustUserTrait { EntrustUserTrait::can as may; Authorizable::can insteadof EntrustUserTrait;}
Reference: Laravel 5.1.11-Trait method can has not been applied, because there are collisions with other trait methods on App \ User