Use ID on newly created Model from ->createOptionAction
For the life of me i can't seem to figure this out.
I have a select form that uses ->createOptionForm so that i can create a new one (company in this case) as well. I want to perform some actions on it after creating like i can do with the afterCreate() method on my create page.
So far i have:
No matter what i try my Model in the ->after() method is always null ($action->after(function ($data, Model $record) {).
8 Replies
That's because the relationships are added after the company is created.
What you can do, is add an obsever to the relationship model and on those being created run a method?
Even though i'm not trying to do anything on the relationship? I just want to do something with the data of the newly created company.
Also, i need form data in the method i use afterwards. Don't know how to get the form data to the observer.
Can you please explain in detail what you are trying to achive? Of you create the company you have the data.. if you want to store to another relationship nest it in the form for the company.
Well, I think the issue lies in how you are attempting to retrieve the model in the ->after() method of your createOptionAction.
Specifically, the Model $record parameter is null because the $action->after() callback does not automatically provide the created model record.
Yeah, ill try to explain it in more detail. I have a form on my Project Model. This form has a select option to select a company to be associated with it. It also has a ->createOptionForm to create a new Company Model.
On my regular Company model i use afterCreate to send the form data over to an api, retrieve an ID and finally, I store all the form data + the newly retrieved ID on the Company. This works fine but i can't seem to get this working from within the ->createOptionForm() method.
I hope i'm being clear.
Any idea how i would het the $record in the after() method or are there any other ways?
maybe it works.
->createOptionAction(function ($action) {
$action->mutateFormDataUsing(function ($data) {
$data['team_id'] = Filament::getTenant()->id;
$data['normalized_title'] = NameNormalizer::normalize($data['title']);
$data['client_number'] = Company::generateUniqueClientNumber();
$data['street'] = ucwords(strtolower($data['street']));
$data['town'] = ucwords(strtolower($data['town']));
$data['postbox_town'] = ucwords(strtolower($data['postbox_town']));
return $data;
});
$action->after(function ($data) {
$company = Company::create($data);
});
});
I am not sure if if works on your sideSo I wouldn't tend to use afterCreate for that when you need multiple creation points. It makes more sense to create an observer for that. So it runs in the background and does not matter how the company is created.
You then have 1 place for your logic and 1 creation method.
Yeah, i was in a rush so i decided to take the easy way out bu tin the end it backfired. Should've gone with an observer in the first place. I refactored to an observer. Thanks for the help.