1

Please i am having an issue generating a uuid as primary key in my user model. i always PHP Error: Class 'App/Traits/boot' not found in C:/xampp/htdocs/twingle/app/Traits/UsesUuid.php on line 11. Tried various method but this error persist

User Model (App\User)


<?php

namespace App;

use Illuminate\Notifications\Notifiable;
use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Foundation\Auth\User as Authenticatable;
use App\Traits\UsesUuid;

class User extends Authenticatable
{
    use Notifiable,UsesUuid;

    protected $keyType = 'string';

    public $incrementing = false;

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [
        'name', 'email', 'password',
    ];

    /**
     * The attributes that should be hidden for arrays.
     *
     * @var array
     */
    protected $hidden = [
        'password', 'remember_token',
    ];

    /**
     * The attributes that should be cast to native types.
     *
     * @var array
     */
    protected $casts = [
        'email_verified_at' => 'datetime',
    ];
}

which use a Trait

UseUuid(App\Traits)

<?php

namespace App\Traits;
use Ramsey\Uuid\Uuid;


trait UsesUuid
{
  public static function UsesUuid()
    {
        boot::creating(function ($model) {
            $model->setAttribute($model->getKeyName(), Uuid::uuid4());
        });
    }
}

User mIgration

    public function up()
    {
        Schema::create('users', function (Blueprint $table) {
            $table->uuid('id')->primary();
            $table->string('name');
            $table->string('email')->unique();
            $table->timestamp('email_verified_at')->nullable();
            $table->string('password');
            $table->rememberToken();
            $table->timestamps();
        });

Please any help will be deeply appreciated. thanks

mykelcodex
  • 25
  • 6
  • The issue isn't to do with creating a primary key, it's just that there is no `boot` class within the `App\Traits` namespace. So the error you need to clear is the `boot::creating...` line in `UseUuid`. – lufc Aug 09 '19 at 14:06
  • You might mean `static::creating` instead of `boot::creating` (based on a quick search, I don't use Laravel much) – Patrick Q Aug 09 '19 at 14:07
  • yep. Or use a Model Observer instead and pick the creating() event. https://laravel.com/docs/5.8/eloquent#observers – jtwes Aug 09 '19 at 14:08
  • `static::creating` would go in the `boot()` method. Observers is cleaner. – lufc Aug 09 '19 at 14:09

2 Answers2

0

Your trait code looks really inconsistent. It should look something like this:

namespace App\Traits;
use Ramsey\Uuid\Uuid;


trait UsesUuid
{
    protected static function boot()
    {
        parent::boot();

        static::creating(function ($model) {
            $model->setAttribute($model->getKeyName(), Uuid::uuid4());
        });
    }
}

That way when the trait is used in a model, it will automatically hook into that model's creating event, and will make sure the primary key is generated as a UUID.

Bogdan
  • 43,166
  • 12
  • 128
  • 129
-1

Go for a model Observer. It's much cleaner and the perfect fit. https://laravel.com/docs/5.8/eloquent#observers

jtwes
  • 144
  • 6
  • While using an observer is a good alternative, I think it should be pointed out that if you need more than one model to use UUIDs you need to define an observer for each model, so in that case the OP's trait approach is more flexible. – Bogdan Aug 09 '19 at 14:15
  • Answers should include any code required for the OP to solve their problem. Link-only answers are strongly discouraged. What you have posted is better-suited to a comment. – Patrick Q Aug 09 '19 at 14:16