How do you manipulate the save/attach methods on filament?
I have a relationsmanager recipe_ingredient I want to convert quantities to metric and imperial on save/edit. here is my attempt but i get nothing:protected $table = 'recipe_ingredient';
public static function boot()
{
parent::boot();
static::creating(function (RecipeIngredient $model) {
Log::info('Creating event fired.');
$model->quantity = $model->quantity * 2;
Log::info('New quantity: '.$model->quantity);
});
static::updating(function ($model) {
Log::info('Updating event fired.');
dd($model);
$model->quantity = $model->quantity * 2;
Log::info('New quantity: '.$model->quantity);
});
}
12 Replies
where is this boot method from? are you using chatgpt or some blindly copy paste code from google search?
chatgpt
well then good luck
read #✅┊rules 5
very helpful dude, im just asking for direction
The direction is to read the doc
If you have read the doc for relation manager, you would have saw it mentioned how to customize actions and it’s linked to here https://filamentphp.com/docs/3.x/actions/prebuilt-actions/create/
i was just having same problem , it's kinda tricky since in the docs it doesn't mention how to do it to your resources , you just need to go to your resource folder ( ex from here: RecipeIngredientResource ) and just under pages folder you have youre crud files , just instead of dealing with the actions as buttons you have the whole same logic in there 😉 hope it helps you
thanks will look into it
hope it helped
just to get it right. I have an :RelationManagers\IngredientsRelationManager::class, in the RecipeResource. do I need to create a resource for the RecipeIngredient pivot table? currently I do not have a RecipeIngredientResource. and there change the create action? because in the relationmanager I am attaching it via : ->headerActions([
// ...
AttachAction::make()
->modalHeading('Add Ingredient')
->modalDescription('Add ingredients, quantities and units')
->modalSubmitActionLabel('Add')
->attachAnother(false)
->preloadRecordSelect()
->label('Add Ingredient')
->form(fn (AttachAction $action): array => [
$action->getRecordSelect(),
Forms\Components\TextInput::make('quantity')->required(),
Select::make('unit')
->label('Unit')
->options(Unit::query()->pluck('unit', 'symbol'))
->required(),
]),
])
thanks, I want to mutate after attaching a record, can't seem to find this in the docs, the createaction updates the ingredients model and not the pivot model recipeingredient.
in the I have a IngredientsRelationManager:: class inside the RecipeResource.