Delete values from dependant select
I have the following code:
It works correctly but if for example I delete a record from the first select, the related records from the second select are not deleted. How could I do it?
Thank you
10 Replies
Are you talking about what appears on the Selects, or what happens to the pivot tables (I'm assuming that 'championships' select is also a multiple()) after your form has been saved, and elsewhere in your app you delete one of the 'nations' from the nations table?
Like, are you expecting the 'championships' pivots that were dependent on the deleted nation to now disappear from the pivot between 'championships' and the model your form is on?
I have the tables of nations and championships, and they have polyform many-to-many relationships.
Then I have an event form with 2 select. In the first I select the nations and the other shows the championships of the selected nations and it is multiple.
What happens to me is that when I delete a nation from the first select the championships of that nation are not deselected in the second select
It would be interesting that if I delete a nation from the main table, the championships related to that nation would be eliminated from the events, but it rarely happens and as I understand it, this cannot be done with mysql, it would only eliminate nation-championships from the nationables pivot table, but no of the championshipables
Ah, you mean when you remove a selection from the nations Select. I thought you meant when you delete a record.
That should work. Is the code you quoted in your OP correct, so I can duplicate it to test? Like, seems as if the 'championships' Select should also be multiple()?
Yes, it is multiple selection, I just added in the first post as I have the two select.
OK. I'll test locally and get back to you.
I can't repliacte this. I remembered I actually have a similar dependent select on a pair of pivots ('districts' and 'regions'), like this:
... and as you can see, they happily work. When removing a District from the selection, the associated Regions disappear.
The only real difference in mine is I don't use the whereHas().
Oh, and I'm running a much earlier v3 version. I guess I need to try it on the latest v3 release.
That works fine for me too. Where I have the problem is after, once the values are selected in the second select, it removes one from the first one and in the second continue the values related to the removed value.
Ah, OK. Now I understand what you mean. Yeah, I think you'll have to handle that yourself.
I spent some time last night trying to resolve how to do this, and the code that should work seems to have issues, and I'm not sure why.
Now I understand the issue you are asking about, it's actually an issue I raised about a year ago when I first started with Filament.
I'll keep trying, and get back to you.
And just fyi, here's the code I'm working on, in an afterStateUpdated() on my 'districts' select (which equates to your 'nations'). It's all split up because I was trying to track why it's not doing exactly what it should.
So the idea is, get the current 'regions' selections, get the new set of Regions bounded by the new state of the districts selection, and set the Regions state to the intersection of that (so remove any that are no longer valid for the new district selection).
It works ... until it doesn't. Still trying to figure out why sometimes it doesn't work.
you can try
->reactive()
It is reactive, see code a few msgs earlier.