Entrust Trait conflict resolution

Source: Internet
Author: User
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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.