teach we about query logic in filament
excuse me, I want to create new data but in the input for a unique code that is generated according to its own format and based on data in the database
when we make it using laravel on the controller, we still understand, but because we are still beginners to determine the query on the filament resource, please give directions to us how to make a kind of controller but on the filament
(and also to be honest because there are still a few tutorials on the internet to help us make a query logic on filament)
The following is an example of the unique code that you want to generate
from that example, how to implement at filament when create new data
8 Replies
Filament
Creating records - Resources - Admin Panel - Filament
The elegant TALL stack admin panel for Laravel artisans.
TextInput::make('booking_code')
->mutateFormDataUsing(function (array $bookingCode): array {
$prefix = 'ORD-PS-';
$latestBooking = Booking::orderBy('id', 'desc')->first();
if ($latestBooking) {
$bookingNumber = intval(substr($latestBooking->booking_code, strlen($prefix)));
$bookingNumber++;
} else {
$bookingNumber = 1;
}
// Buat kode booking dengan format "ordps0001"
$bookingCode = $prefix . str_pad($bookingNumber, 4, '0', STR_PAD_LEFT);
return $bookingCode;
})
i try it and i got an error
Method Filament\Forms\Components\TextInput::mutateFormDataUsing does not exist.
how to fix this error sir?
mutateFormDataUsing() goes on an action not on a form component. If you want to do it on a form component instead of when the entire form is saved then you’ll need to look at hydration. https://filamentphp.com/docs/2.x/forms/advanced#field-lifecycle
TextInput::make('booking_code')
->afterStateHydrated(function () {
$prefix = 'ORD-PS-';
$latestBooking = Booking::orderBy('id', 'desc')->first();
if ($latestBooking) {
$bookingNumber = intval(substr($latestBooking->booking_code, strlen($prefix)));
$bookingNumber++;
} else {
$bookingNumber = 1;
}
$bookingCode = $prefix . str_pad($bookingNumber, 4, '0', STR_PAD_LEFT);
})
like this? im sorry im very very very beginner
Close, but you’ll have to inject the component into the the function and end the function by setting the state with $component->state($bookingCode)
Look closely at the example in the doc.
thanks for your example.
i've done it.
🎉
For the next time: please read #✅┊rules on how to ask and do code formatting.