Mutate form data before create but for delete

protected function mutateFormDataBeforeCreate(array $data): array
{
$data['date_issued'] = date(now());
$amount = $data['stock_issued'];
$stock = DB::table('inventories')->where('id', $data['inventory_id'])->value('stock');
if ($amount > $stock) {
Notification::make()
->warning()
->title('You don\'t have enough Stock!!!')
->body('Only ' . $stock . ' in inventory.')
->send();
$this->halt();
} else {
DB::table('inventories')->where('id', $data['inventory_id'])->update(['stock' => $stock - $amount]);
}
return $data;
}
protected function mutateFormDataBeforeCreate(array $data): array
{
$data['date_issued'] = date(now());
$amount = $data['stock_issued'];
$stock = DB::table('inventories')->where('id', $data['inventory_id'])->value('stock');
if ($amount > $stock) {
Notification::make()
->warning()
->title('You don\'t have enough Stock!!!')
->body('Only ' . $stock . ' in inventory.')
->send();
$this->halt();
} else {
DB::table('inventories')->where('id', $data['inventory_id'])->update(['stock' => $stock - $amount]);
}
return $data;
}
I use the above code to update the inventory when inventory is issued out, I was wondering if there is a function like this but for the delete button so I can undo the inventory update if a record from issued inventory is deleted.
Solution:
Yup That works, Thank You.
Jump to solution
8 Replies
DrByte
DrByte12mo ago
I'm pretty sure there is not such a method, because Filament simply passes through the $record->delete() to the Model directly. https://github.com/filamentphp/filament/blob/751c24beb998e334803701ed48a379031256fbaf/packages/actions/src/DeleteAction.php#L48-L58
GitHub
filament/packages/actions/src/DeleteAction.php at 751c24beb998e3348...
A collection of beautiful full-stack components for Laravel. The perfect starting point for your next app. Using Livewire, Alpine.js and Tailwind CSS. - filamentphp/filament
DrByte
DrByte12mo ago
So you probably have two options: a. clone that DeleteAction and add your own custom logic before the actual delete. b. fire an Observer or register a static::deleting event on the Model, to do your inventory reversal
Laravel - The PHP Framework For Web Artisans
Laravel is a PHP web application framework with expressive, elegant syntax. We’ve already laid the foundation — freeing you to create without sweating the small things.
The Huntsman
The HuntsmanOP12mo ago
what about the after() and before() methods that's available on the delete action ? https://filamentphp.com/docs/3.x/actions/prebuilt-actions/delete#lifecycle-hooks
The Huntsman
The HuntsmanOP12mo ago
is it possible for that function to get the data of the record about to be be deleted, like the $data array for mutateFormDataBeforeCreate()
DrByte
DrByte12mo ago
Yes that would make sense. Most of the time in Filament you can use fn (Model $record) to get the current record. That's not listed on the Actions page in the docs https://filamentphp.com/docs/3.x/actions/advanced ... but it is in the Table docs. https://filamentphp.com/docs/3.x/tables/actions#table-action-utility-injection
Solution
The Huntsman
The Huntsman12mo ago
Yup That works, Thank You.
The Huntsman
The HuntsmanOP12mo ago
Here's the code if anyone else is interested
Actions\DeleteAction::make()
->before(function (Model $record) {
$inventory_id = $record['inventory_id'];
$amount = $record['stock_issued'];
$stock = DB::table('inventories')->where('id', $inventory_id)->value('stock');
$qb = DB::table('inventories')->where('id', $inventory_id)->update(['stock' => $stock + $amount]);
if ($qb == true) {
Notification::make()
->success()
->title('Inventory Updated.')
->send();
} else {
Notification::make()
->warning()
->title('Error')
->body('There was a problem updating inventory.')
->send();
$this->halt();
}
}),
Actions\DeleteAction::make()
->before(function (Model $record) {
$inventory_id = $record['inventory_id'];
$amount = $record['stock_issued'];
$stock = DB::table('inventories')->where('id', $inventory_id)->value('stock');
$qb = DB::table('inventories')->where('id', $inventory_id)->update(['stock' => $stock + $amount]);
if ($qb == true) {
Notification::make()
->success()
->title('Inventory Updated.')
->send();
} else {
Notification::make()
->warning()
->title('Error')
->body('There was a problem updating inventory.')
->send();
$this->halt();
}
}),
DrByte
DrByte12mo ago
Excellent! (please also mark that post as "the answer": right-click it, choose "Apps" and "Mark Selected")
Want results from more Discord servers?
Add your server