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
We would need to see your code, but why are you not using validation and checking that this record is unique before creating?
Already handled it at input field.
sample code
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
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
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
partIT 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
Maybe Ill just go with manual edit nor create process to prevent overcomplicate things on my part. Thanks @toeknee_iom !
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
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
That make sense, thats why it creates and also updates present data. Is there a best way to approach it?
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
Also tried using before
So it’s your bug
Doesn’t matter
You are executing creations before filament
If you just want to prevent dups
So how can I approach it so that I can do updateOrCreate?
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
I just past the logic onto after hook?
Should be
Filament
Validation - Form Builder - Filament
The elegant TALL stack form builder for Laravel artisans.
Thanks! Your explanation is super helpful