Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Synchro's avatar

Nova missing field on create

I have a Nova resource with these fields:

            Text::make('Name'),
            Textarea::make('Description')
                ->nullable(),
            Text::make('UUID')
                ->default(
                    function () {
                        return Uuid::uuid4();
                    }
                )
                ->readonly()
                ->onlyOnDetail(),

The underlying model has these fields marked as fillable:

    protected $fillable = [
        'name',
        'description',
        'uuid',
    ];

The UUID is auto-generated via the closure in default and is populated if I show it on forms.

But when I hit "create", I get an error because the uuid field is not included in the INSERT statement and has no default value:

SQLSTATE[HY000]: General error: 1364 Field 'uuid' doesn't have a default value (SQL: insert into `things` (`name`, `description`, `updated_at`, `created_at`) values (Case, ?, 2021-07-23 18:07:28, 2021-07-23 18:07:28))

I'm pretty sure I have defined the field correctly as I use the same pattern on another resource and it works fine there. I've cleared caches, and if I make other changes to fields, they show up.

Why might the UUID field be going missing?

0 likes
16 replies
bugsysha's avatar

What happens when you remove ->readonly()->onlyOnDetail()?

Synchro's avatar

Nothing, still the same error. I'm pretty sure the readonly config only affects how it is presented in the Nova UI, not the SQL it generates. I have another resource that's set up with readonly the same way and it works fine for generating UUIDs, but I can't spot what's different about it.

As an alternative, is there some central place I can have Laravel automatically generate UUIDs for models so that a standard call to create will make one?

bugsysha's avatar

What happens if you replace Text::make('UUID') with Text::make('Uuid')? Or replace Text::make('UUID') with Text::make('UUID', 'uuid')?

Synchro's avatar

The working one is using Text::make('UUID') but I tried Text::make('UUID', 'uuid') as I was also wondering about case sensitivity. In both tables the field is called uuid, lower case.

I found that if I remove both readonly and onlyOnDetail, then it populates the field with a default value and saves it. The only problem with that is that the UUID is then editable, which it really shouldn't be.

I have now found another field type in Nova: Hidden. This allows me to use the default value (the docs even recommend it for that), however, I want the field to be shown on the detail view and be searchable, but it seems that adding ->showOnDetail() doesn't override its hidden nature:

            Hidden::make('UUID')
                ->default(
                    function () {
                        return Uuid::uuid4();
                    }
                )
                ->showOnDetail(),

So I seem to have a choice between visible and editable, and invisible and never shown, but I can't have visible and not editable.

bugsysha's avatar

I found that if I remove both readonly and onlyOnDetail, then it populates the field with a default value and saves it.

Don't want to brag, but just as I suggested in the first post 🤣

Synchro's avatar

I know, and as I said this does not solve the problem. Overall I do not have a solution to this issue yet.

Synchro's avatar

I don't want the UUID to be editable (why would I?), and if I make it have a default value and be readonly, it still gets omitted from the query, so showing it doesn't help. Similarly if I don't set readonly but call hideWhenCreating, it also gets omitted from the query.

bugsysha's avatar
bugsysha
Best Answer
Level 61

@synchro why do you need to show the UUID? The point is to show it only when the resource is created. Put readonly() flag to it. Add to your model something like:

    protected static function booted()
    {
        static::creating(function ($user) {
            $user->uuid = Uuid::uuid4();
        });
    }

You have more in the documentation on how to do this in various ways so you can pick the one that suits you best.

Synchro's avatar

Thanks. This is exactly what I was wondering when I said:

is there some central place I can have Laravel automatically generate UUIDs for models so that a standard call to create will make one?

I didn't realise how that worked, and now I can work around this Nova limitation with that – and it works very nicely.

bugsysha's avatar

I thought you knew about that and you were looking for a Nova-based solution. Glad it works. You are very welcome. Thanks for the "Best Answer".

Synchro's avatar

Well I was looking for a Nova-based answer, and I still think there is a bug or omission of sorts in there, however, I want this behaviour to happen everywhere that these models are created, not just in Nova. I am already using dyrynda/laravel-model-uuid, but it turned out I was using it wrong – your pointer has allowed me to delete lots of unnecessary code and use this package as it was intended!

bugsysha's avatar

@synchro do you need a package for such a simple thing? Especially considering that there is Uuid class that can generate them that is bundled with Laravel out of the box.

Synchro's avatar

I'm not sure what you're referring to? I'm using the uuid field type in Eloquent, and that package was a convenient way to set up several other UUID-related things (I have about 12 models using them, so a trait was a useful approach), but I've not seen any particular native Laravel support beyond that.

bugsysha's avatar

There is \Ramsey\Uuid\Uuid::uuid4(). It is bundled with Laravel for as long as I can remember. You just need to create a trait with a single method and add it to your models.

Synchro's avatar

Yes, that's what I'm using, but it only deals with technical UUID generation, not using them for other things in a Laravel context.

Please or to participate in this conversation.