Using Select with a BelongsToMany relation
Hi,
I recognised a weird issue in an appliation of mine. I am using a
Select
component to assign Rooms
to a Contract
. They are stored in a contract_room
table since it's a many-to-many relationship.
When we take a look into the code, it seems like n:m is supported by Select
.35 Replies
When I use
Select
and add the specific rooms, the hotel rooms are getting cleared and no data are stored within contract_room
.It's kinda odd, that the select knows, which data to use - so I assume the
relationship()
function is applied on the hotel object instead of the contract object.
That's my only explanation for this behaviour. But: When I add dd($relationship->getTable())
it shows contract_room
. So I am kinda clueless what's going on here.Just to make sure again: It's a
BelongsToMany
relation, not HasMany
?so I assume the relationship() function is applied on the hotel object instead of the contract object.Why do you think it's applied to Hotel? This is a ContractResource, right?
Yes, it's the ContractResource. I assume that it's applied to hotel because when I select the rooms and hit "Save", the rooms of the selected hotel (in tab "Vertrag") are getting wiped and there are no persisted data within
contract_room
.Can you share the resource form?
All of it?
It's pretty long
All form fields: https://gist.github.com/alexgaal/f4e03fd86242709616464abe07ef650c
Line 115 - 122
You are using the same relation 2 times:
rooms
and hotel-rooms
Ah, it's conditionalYes, one of them is used to add prices for specific time ranges
First the user should add rooms to the contract from the hotel and afterwards they should price it.
But they both are shown at the same time?
Both of these fields are trying to save to the same relationship, so I guess you are having a conflict there.
Ah... wait, I see.
I attach my rooms at
Zimmer
(where the select component is located) and Filament is saving hotel-rooms
as well?
It's hidden, but still get's saved, yeah?Oh it is hidden. Thought they are using the same condition. Well, it shouldn't save when it's hidden š¤
But only the Tab is hidden.
The component of
hotel-rooms
itself isn't.Just to make sure: Remove the second one and try whether it works.
You have a name conflict. You reuse
rooms
in the "Reduktionen" tab.I renamed it to
reduction-rooms
now and made the Repeaters hidden on the same condition when the tab is hidden, I'll now try to add a room
The issue is still present: The hotel lost it's rooms and no rooms are attached to the contract. :/Can you isolate this more? Just required fields and the rooms select?
Sure, give me a few moments
I removed all the following tabs (these are including the Repeaters with rooms relationship) and left: Contract, Seasons and Rooms, because these are needed. I added a room to the hotel and refreshed everything.
š It's working.
I'll now slowly add every tab again, to see which one is causing this issue.
price-rooms
is definitely causing this issue. š
Is there a recommended way of my use case? I need to add rooms to a contract from a hotel and assign prices and reductions to these rooms individually.
The customer wants the rooms selected within the tabs workflow instead of a relation manager. šYou mean,
hotel-rooms
? Can't find price-rooms
Yes, I renamed that to
price-rooms
so it's clearer. šI think the issue is that Repeater is for
HasMany
not BelongsToMany
Yes and No. š Season - Room relation is implemented via Pivot, but Contract - Room isn't.
š If I implement this, I can't use the select component because it doesn't support HasMany. š
The client will have to arrange to use a relation manager, it is what it is. š
You can override the save behaviour instead of using the default one
I guess it can made to work with your requirements. Just the default won't work, because you have two different data structures that you want to save in the same relation
I'll note that for a later time of the project. Launch is planned end of October and this requirement isn't blocking the functionality, so the client will have to arrange with using a relation manager for now if they want to go live by November. š I'll investigate that use case and if I can solve it I'll provide a PR.
Thank you for your help š„
I don't think this needs a PR. That doesn't sound like something that should work out of the box.
One last question about this: Can I enforce a refresh after attaching a record via a relation manager?
Should be possible to trigger a refresh via an emit and a listener on the page that executes
$refresh
Merci