F
Filament12mo ago
Robin

accessing create functions inside a custom filament page

How can I access(use) resource create functions such as mutateFormDataBeforeCreate and afterCreate
Solution:
If it's a custom page there is no afterCreate() etc. because you have full control over the form. You need to implement the save logic yourself
Jump to solution
6 Replies
Dennis Koch
Dennis Koch12mo ago
I don't understand your question
Robin
RobinOP12mo ago
I needed a create page on the navbar, so I created a new custom page, and copy pasted the from in resourceA, in the resource A, I had some lifecycle hooks functions on the CreatePage of resource A such as afterCreate() which updated the value of another field with the created field
Solution
Dennis Koch
Dennis Koch12mo ago
If it's a custom page there is no afterCreate() etc. because you have full control over the form. You need to implement the save logic yourself
Robin
RobinOP12mo ago
so, I've got this function, it's succesfully saved but when updating the $new_qty nothing happens, I needed it to decrement the 'qty' with $new
public function create(): void
{
$data = $this->form->getState();
$record = Sale::create($data);

$sale_id = $record->id;
// dd($sale_id);
$saleStores = SaleStore::where('sale_id', $sale_id)->get();
foreach ($saleStores as $saleStore) {
$s_qty = $saleStore->selected_qty;
$store_id = $saleStore->store_id;
$store_qty = Store::find($store_id)->qty;
$new_qty = $store_qty - $s_qty;
Store::find($store_id)->update(['qty' => $new_qty]);
}

$record->balance_price = $record->sub_total;
$record->update(['balance_price_price' => $record->partial_price]);

$this->form->model($record)->saveRelationships();
$this->form->fill();
}
public function create(): void
{
$data = $this->form->getState();
$record = Sale::create($data);

$sale_id = $record->id;
// dd($sale_id);
$saleStores = SaleStore::where('sale_id', $sale_id)->get();
foreach ($saleStores as $saleStore) {
$s_qty = $saleStore->selected_qty;
$store_id = $saleStore->store_id;
$store_qty = Store::find($store_id)->qty;
$new_qty = $store_qty - $s_qty;
Store::find($store_id)->update(['qty' => $new_qty]);
}

$record->balance_price = $record->sub_total;
$record->update(['balance_price_price' => $record->partial_price]);

$this->form->model($record)->saveRelationships();
$this->form->fill();
}
Dennis Koch
Dennis Koch12mo ago
Did you debug $saleStores? Why should there be SalesStores when you just created that Sale? Is that via an Observer?
Robin
RobinOP12mo ago
the salesStores is a pivot table between sales and store, when I dd it returns an empty array 😅
Illuminate\Database\Eloquent\Collection {#2876 ▼ // app/Filament/Pages/NewSale.php:210
#items: []
#escapeWhenCastingToString: false
}
Illuminate\Database\Eloquent\Collection {#2876 ▼ // app/Filament/Pages/NewSale.php:210
#items: []
#escapeWhenCastingToString: false
}
yep, this is it, just solved it after moving
$this->form->model($record)->saveRelationships();
$this->form->model($record)->saveRelationships();
before the foreach loop Many thanks!
Want results from more Discord servers?
Add your server