Filament form save ignores eloquent updateOrCreate?

As the title says, I'm trying laravel updateOrCreate eloquent as a way to detect duplicates on data creation. The method works like I want it to be, it updates the current model data but my problem is, it creates a new record prior on just updating the data. Does filament bypass this method or am I missing something on my codebase? I'm using https://filamentphp.com/docs/2.x/admin/resources/creating-records, before method on CreateAction since I want the array of data being passed on form's input field. Am I doing something wrong here? Any input would be much appreciated!
Filament
Installation - Admin Panel - Filament
The elegant TALL stack admin panel for Laravel artisans.
18 Replies
toeknee
toeknee2y ago
We would need to see your code, but why are you not using validation and checking that this record is unique before creating?
Resonance
ResonanceOP2y ago
Already handled it at input field. sample code
->afterFormValidated(function (array $data): array {
$medicineInventory = MedicineInventory::updateOrCreate(
['generic_name' => $data['generic_name'], 'dosage_form' => $data['dosage_form']],
['stock' => $data['stock']] // and the rest of the data in the form field
);
->afterFormValidated(function (array $data): array {
$medicineInventory = MedicineInventory::updateOrCreate(
['generic_name' => $data['generic_name'], 'dosage_form' => $data['dosage_form']],
['stock' => $data['stock']] // and the rest of the data in the form field
);
When im using dd, updated data are there, but with all of the data inputted on the form field. So it updates, and creates at the same time instead of just updating if first parameter finds the data in the db
toeknee
toeknee2y ago
The model naturally will only update what is changed it's why you have an original option on the model so it can compare the record changes and updte
Resonance
ResonanceOP2y ago
It does that, when I observed the POST datas on dd, but it also creates new data on update. Can I return the function to null? nvm, it gave me an error on :array part
toeknee
toeknee2y ago
IT shouldn't create, but also you shouldn't need updateOrCreate? They either edit or create, there are difference options usually and if you allow updating a form based of a value you'll end up possibly overwritting data
Resonance
ResonanceOP2y ago
Maybe Ill just go with manual edit nor create process to prevent overcomplicate things on my part. Thanks @toeknee_iom !
toeknee
toeknee2y ago
This is why in resources we use a resouroce, since it uses one form for edit and create to simplify things but each action can be conditioned
wyChoong
wyChoong2y ago
Firstly, filament form only uses create() and update() in create and edit form respectively. Never updateOrCreate() In this code, you are adding hook after the form validation In a normal form submit lifecycle is Submit > validate > save/create
Resonance
ResonanceOP2y ago
That make sense, thats why it creates and also updates present data. Is there a best way to approach it?
wyChoong
wyChoong2y ago
You adding afterFormValidated, which essentially is in between validate and create And in that hook, you are doing updateOrCreate, before filament even reaches its create
Resonance
ResonanceOP2y ago
Also tried using before
wyChoong
wyChoong2y ago
So it’s your bug Doesn’t matter You are executing creations before filament If you just want to prevent dups
Resonance
ResonanceOP2y ago
So how can I approach it so that I can do updateOrCreate?
wyChoong
wyChoong2y ago
In the after validate hook, call Laravel validation to validate that 2 fields combination doesn’t exist Then let filament handle the creation if it passes
Resonance
ResonanceOP2y ago
I just past the logic onto after hook?
wyChoong
wyChoong2y ago
Should be
wyChoong
wyChoong2y ago
Resonance
ResonanceOP2y ago
Thanks! Your explanation is super helpful

Did you find this page helpful?