F
Filament6mo ago
Archie

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 Koch6mo ago
I don't understand your question
Archie
Archie6mo 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 Koch6mo 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
Archie
Archie6mo 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 Koch6mo ago
Did you debug $saleStores? Why should there be SalesStores when you just created that Sale? Is that via an Observer?
Archie
Archie6mo 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
More Posts
Table CreateAction modal on a view page pops and disappears on v3.1.27.I have a createaction on a table inside a view page .I recently upgraded to v3.1.27 .When clicked , Searchable and sortable on polymorphHi, how to implement a search based on a polymorphic relation with a relationship one down even? Wispatie-laravel-media-library-plugin events ?Hi everyone, i'm using spatie-laravel-media-library-plugin in a form and i try to handle media addeHow to show a different Filament Panel based on the user role?I am using Spatie's Laravel Permission. Is there a way to show a different Filament Panel based on tHow to populate multi select list with default selected valuesI have this multiple select form field where I load the options and try to set the default values: How do you handle models with custom route keys?I have articles, their route key is a slug column - I can't see anywhere in the filament docs how toMethod Filament\Clinic\Resources\ClinicResource\Pages\CheckOutRxOrder::getColumnSpan doesn't Exist<?php namespace App\Filament\Clinic\Resources\ClinicRxOrderResource\Widgets use Filament\Widgets\WidCreate tenant menuI'm trying to limit the number of tenants that a user can create, I have this code inside TeamPolicyHow do I make EditProfile.php use more width please?I am using custom editprofile.php with Filament panel. The EditProfile page is using small width, PlHeader Table SortByI cannot for the life of me figure out in the docs where to replicate the sort by found on this page