Search code examples
phplaraveluuidlaravel-5.8entrust

Double boot function causes trait collusion


The laravel debugger is showing me this error:

Trait method boot has not been applied, because there are collisions with other trait methods on App\User

If i remove one of those traits, Uuids or Entrust the error doesn't show. But i need them both. So how can i fix this?'

I am using laravel 5.8

Thanks in advance!

namespace App;
use Zizaco\Entrust\Traits\EntrustUserTrait;
use Illuminate\Foundation\Auth\User as Authenticatable;

class User extends Authenticatable
{

    use Uuids;
    use EntrustUserTrait;

}

trait Uuid

   <?php
namespace App;

use Webpatser\Uuid\Uuid;

trait Uuids
{
    /**
     * Boot function from laravel.
     */
    protected static function boot()
    {
        parent::boot();
        static::creating(function ($model) {
            $model->{$model->getKeyName()} = Uuid::generate(5,'test', Uuid::NS_DNS);
        });
    }
}

i think is has to do something with the boot function


Solution

  • <?php
    namespace App;
    
    use Webpatser\Uuid\Uuid;
    
    trait Uuids
    {
        /**
         * Boot function from laravel.
         */
        protected static function bootUuids()
        {
            static::creating(function ($model) {
                $model->{$model->getKeyName()} = (string) Uuid::generate(4);
            });
        }
    }
    

    reference: SQLSTATE[42S01]: Base table or view already exists or Base table or view already exists: 1050 Table