How do I generate a random reference?

I've got a resource with a reference field that I want to automatically generate a random identifier for. The user opens the record creation page, and the default value is "INV-0001" or "INV-4829" (haven't decided if I want it to be random or incremented yet). How do I do that?
Solution:
->default(fn() => 'INV-' . Str::random(4) )
->default(fn() => 'INV-' . Str::random(4) )
...
Jump to solution
6 Replies
toeknee
toeknee2d ago
Invoices should be incremental really for accounting reasons. You can start the first one higher if need be. Otherwise as Leandro mentioned. Or via the model creating function you set it on create in the model which would cover your API use case if you ever wanted to automate creating invoices from anywhere.
Raven
RavenOP2d ago
That's not what I want to happen tho. I want the form to by default fill with something like INV-0001 and then the user can change it in the form. Basically, I want it to suggest INV-0001 and the user can change it if need be.
toeknee
toeknee2d ago
So then you want to use ->default with a closure
Solution
toeknee
toeknee2d ago
->default(fn() => 'INV-' . Str::random(4) )
->default(fn() => 'INV-' . Str::random(4) )
Raven
RavenOP2d ago
Perfect, thank you!
Want results from more Discord servers?
Add your server