What happens when you remove ->readonly()->onlyOnDetail()?
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?
@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.
Please or to participate in this conversation.