Problem with two select fields using same relationship
I have a polymorphic relationship of filters, Filters have a column describing its type, lets say A and B
I want 2 select fields on related model forms, using the third parameter of the relationship() method, i scope the results to one kind (A) or another (B)
Example:
The problem is that the last field on the form for the same relationship, in this case "b" is overwriting the value from the "a" select, i would need it to just add its values to the relationship, but not overwriting it, because of this problem, the only usable select field is the last one in the form, which has scoped values to only show one of the kinds of filters
I want to separate the two kinds of filters in different select fields, i know that if i only used one field and didn't scope the options, it would work, but i want to know if there is any way to do it š
17 Replies
the only way to do it is with overriding saveRelationshipsUsing() on each so that it only syncs the relationships for that type
use your ide to look at the saveRelationshipsUsing() implementation that is set by the relationship() method and tweak it as required
Thank you @Dan Harrin ! im going to check it out
Hmm for some reason, when leaving empty both of the select inputs im getting the $state variable of the callbacks filled with id's that arent present visually in the select field
probably because the ids there arent scoped or something
sorry i was a bit slow to share the photo lol
oh...
i dont understand, ill take a look tho
oh that is weird yes
but it looks like the ids are repeated
yeah pretty weird
yeah i dont know where those would come from
maybe you need a custom
loadStateFromRelationshipUsing()
toowhat exactly does
pathology()
on the query do?its a local scope @josef it adds ->where('type', 'pathology') to the query
yeah @Dan Harrin probably ill try that too
thank you!
Hi @bosphoramus. Did you managed to get this working? I'm looking at a similar thing here.
Yes @danielplomp
Ill share the code sortly
Thanks. Do you think that will also works with many-to-many, non-polymorphic?
Yup i think so @danielplomp
@danielplomp
I extracted the custom component to a class
so in the resource i just had to do:
TagSelect::make('tag')->label(Town)
i have another component like this for the other type of filters, TownSelect
its the same, just changing Tag
for Town
you can put both in the same resource because they are tweaked so they only sync the filters of its own type
so they wont collide, or overwrite themselves
its a bit hard to explainThanks man. Iām going to take a look šš¼
I've got it working. Thanks!
I created two helpers so I won't repeat myself for every other type of Article:
Now I can just call this method from the Resource:
The only think I would like to know is how I could filter out the already selected items, when you open the Select dropdown. Maybe @Dan Harrin has an idea?
its a bug
we know about
This is something the Relationship already does automatically.
Ah ok!