© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
FilamentF
Filament•2y ago•
10 replies
Pedroesca

beforeSave whith relations

I have the following process in beforeSave() of EditSale.php, in which I restore the stock (quantities) of products, before the records are saved, as indicated by the "beforeSave" hook, but it turns out that when entering this hook the data of the relationship (SaleDetails) has already been saved.

protected function beforeSave(): void
    {
        //antes de guardar, DESHACEMOS EL STOCK
        $originalDetails = $this->record->products->map(function($product) {
            return [
                'product_id' => $product->id,
                'qty' => $product->getOriginal('qty'), // Obtener la cantidad original antes de la modificación
            ];
        });
        dd($originalDetails);
        foreach ($originalDetails as $detail) {
            $product = $detail->product;
            Log::info('revert stock', ['product_id' => $product->id]);
            Log::info('revert stock', ['cant anterior' => $detail->qty]);
            Log::info('revert stock', ['stock anterior' => $product->stock_current]);
            $product->stock_current += $detail->qty;
            $product->save();
        }
        
    }
protected function beforeSave(): void
    {
        //antes de guardar, DESHACEMOS EL STOCK
        $originalDetails = $this->record->products->map(function($product) {
            return [
                'product_id' => $product->id,
                'qty' => $product->getOriginal('qty'), // Obtener la cantidad original antes de la modificación
            ];
        });
        dd($originalDetails);
        foreach ($originalDetails as $detail) {
            $product = $detail->product;
            Log::info('revert stock', ['product_id' => $product->id]);
            Log::info('revert stock', ['cant anterior' => $detail->qty]);
            Log::info('revert stock', ['stock anterior' => $product->stock_current]);
            $product->stock_current += $detail->qty;
            $product->save();
        }
        
    }

i.e. where do i get the original detail before it is saved? is there a hook for this? for these cases beforesave does not work.
Filament banner
FilamentJoin
A powerful open source UI framework for Laravel • Build and ship admin panels & apps fast with Livewire
20,307Members
Resources

Similar Threads

Was this page helpful?
Recent Announcements

Similar Threads

beforeSave()
FilamentFFilament / ❓┊help
3y ago
Relationship Repeater in EditRecord saves before beforeSave?
FilamentFFilament / ❓┊help
3y ago
Importer: beforesave() hook check
FilamentFFilament / ❓┊help
3y ago
Confirm modal inside beforeSave hook?
FilamentFFilament / ❓┊help
3y ago