#how to apply default values to model instance

5 messages · Page 1 of 1 (latest)

boreal field
#

I have some values that are set by default.

        Schema::create('users', function (Blueprint $table) {
            $table->uuid('id')->primary()->default(Str::uuid());
            $table->string('name');
            $table->text('biography')->nullable();
            $table->string('email')->unique();
            $table->string('password');
            $table->enum('role', ['STANDARD', 'PRO', 'ADMIN'])->default('STANDARD');
            $table->boolean('active')->default(false);
            $table->timestamps();
        });

when I create an instance of the model the role enum is null. I want the field to be set.

Any thoughts?

heavy trout
#

Have you tried to add the attribute property to your model?
something like this

class Product extends Model
{
    use HasFactory;

    protected $attributes = [
        'name' => 'test'
    ];

    protected $fillable = [
        'name',
        'slug',
        'description',
        'price',
        'discount',
    ];
}
boreal field
#

why should I do that and what would be 'test'?

#

the value

#

if I return the $user it has no default values, role is null.