4

In my laravel project, I want set a random default value for each new created record.

According of this Doc, I try this:

use Illuminate\Support\Str;
use Illuminate\Database\Eloquent\Model;

class User extends Authenticatable
{
    protected $fillable = [
       'access_token'
    ];

    protected $attributes = [
        'access_token' => str::uuid()
    ];
}

But I get error for protected $attributes line

"Constant expression contains invalid operations"

3 Answers 3

6

This is because properties cannot contain expressions that they cannot evaluate at compile time. From the official documentation.

Class member variables are called "properties". You may also see them referred to using other terms such as "attributes" or "fields", but for the purposes of this reference we will use "properties". They are defined by using one of the keywords public, protected, or private, followed by a normal variable declaration. This declaration may include an initialization, but this initialization must be a constant value--that is, it must be able to be evaluated at compile time and must not depend on run-time information in order to be evaluated.

Another way of doing this would be through model events. In your User model's boot() method, you can hook into the Creating event. If this method does not exist, create it.

public static function boot()
{
    parent::boot();

    static::creating(function($user) {
        $user->access_token = (string) Str::uuid();
    });
}
Sign up to request clarification or add additional context in comments.

1 Comment

The latest best practice in Laravel is now to use booted() instead of boot() which is also nice in that booted() doesn't require a call to a parent boot/booted method. There's an example in the docs here: laravel.com/docs/9.x/eloquent#events-using-closures
3
protected $attributes = [
    'access_token' => ''
];

public function __construct(array $attributes = [])
{
    parent::__construct($attributes);
    $this->attributes['access_token'] = Str::uuid();
}

In php, it is impossible to call a function from a property

1 Comment

Hello and Welcome to StackOverflow! Would you consider adding some references?
-1

The problem is arising from the way in which you are calling the uuid function.

Since it is static, you will need to access it using;

Str::uuid()

And this will return an object, so in order to get a string out of it, you will have to cast the result.

(string) Str::uuid()

Therefore essentially, your attributes property should be;

protected $attributes = [
    'access_token' => (string) Str::uuid()
];

You can have a look at the docs

2 Comments

No, this won't work. Class properties cannot contain expressions that cannot be evaluated at compile time.
@Mozammil, You are right. I suppose I completely overlooked the obvious and it would make more sense to have this in a creating or created model event as you have proposed. Thank you.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.