insert value from function when create

hello i want to insert new invoice_number from function when create the form. I try using
formatStateUsing
formatStateUsing
seems like no working at all. please help, thank you!
Forms\Components\TextInput::make('invoice_number')
->readOnly()
->unique()
->validationMessages([
'unique' => 'Oops... seems like the :attribute has already been used by other, just generate again!.',
])
->formatStateUsing(function (Forms\Set $set) {
self::generateInvoiceNumber($set);
})
->hintAction(
Action::make('newInvoiceNumber')
->icon('heroicon-s-arrow-path')
->action(function (Forms\Set $set) {
self::generateInvoiceNumber($set);
})
),
Forms\Components\TextInput::make('invoice_number')
->readOnly()
->unique()
->validationMessages([
'unique' => 'Oops... seems like the :attribute has already been used by other, just generate again!.',
])
->formatStateUsing(function (Forms\Set $set) {
self::generateInvoiceNumber($set);
})
->hintAction(
Action::make('newInvoiceNumber')
->icon('heroicon-s-arrow-path')
->action(function (Forms\Set $set) {
self::generateInvoiceNumber($set);
})
),
5 Replies
Tally
Tally7mo ago
maybe check out the ->default() function?
thyk123
thyk123OP7mo ago
still doesnt work :"(
LeandroFerreira
LeandroFerreira7mo ago
are you using a Resource or Custom Page?
thyk123
thyk123OP7mo ago
Resource
php
class InvoiceResource extends Resource
{
Forms\Components\TextInput::make('invoice_number')
->readOnly()
->unique()
->validationMessages([
'unique' => 'Oops... seems like the :attribute has already been used by other, just generate again!.',
])
->formatStateUsing(function (Forms\Set $set) {
self::generateInvoiceNumber($set);
})
->hintAction(
Action::make('newInvoiceNumber')
->icon('heroicon-s-arrow-path')
->action(function (Forms\Set $set) {
self::generateInvoiceNumber($set);
})
),

protected static function generateInvoiceNumber(Forms\Set $set): void
{
$prefix="INV";
$date=now()->format('yymm');
if(Invoice::whereDate('created_at', today())->get()->isEmpty()) //date
{
$set('invoice_number',$prefix.$date.'0001');
}

if(Invoice::latest()->first()== true)
{
$set('invoice_number',(str_pad((int)$latest->invoice_number + 1, 4, '0', STR_PAD_LEFT)));
}

}
}
php
class InvoiceResource extends Resource
{
Forms\Components\TextInput::make('invoice_number')
->readOnly()
->unique()
->validationMessages([
'unique' => 'Oops... seems like the :attribute has already been used by other, just generate again!.',
])
->formatStateUsing(function (Forms\Set $set) {
self::generateInvoiceNumber($set);
})
->hintAction(
Action::make('newInvoiceNumber')
->icon('heroicon-s-arrow-path')
->action(function (Forms\Set $set) {
self::generateInvoiceNumber($set);
})
),

protected static function generateInvoiceNumber(Forms\Set $set): void
{
$prefix="INV";
$date=now()->format('yymm');
if(Invoice::whereDate('created_at', today())->get()->isEmpty()) //date
{
$set('invoice_number',$prefix.$date.'0001');
}

if(Invoice::latest()->first()== true)
{
$set('invoice_number',(str_pad((int)$latest->invoice_number + 1, 4, '0', STR_PAD_LEFT)));
}

}
}
Want results from more Discord servers?
Add your server