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
alexanderkroneis
alexanderkroneisOPā€¢14mo ago
No description
alexanderkroneis
alexanderkroneisOPā€¢14mo ago
When I use Select and add the specific rooms, the hotel rooms are getting cleared and no data are stored within contract_room.
alexanderkroneis
alexanderkroneisOPā€¢14mo ago
No description
alexanderkroneis
alexanderkroneisOPā€¢14mo ago
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.
Dennis Koch
Dennis Kochā€¢14mo ago
Just to make sure again: It's a BelongsToMany relation, not HasMany?
alexanderkroneis
alexanderkroneisOPā€¢14mo ago
Contract.php

public function rooms(): BelongsToMany
{
return $this->belongsToMany(Room::class);
}
Contract.php

public function rooms(): BelongsToMany
{
return $this->belongsToMany(Room::class);
}
Room.php

public function contracts(): BelongsToMany
{
return $this->belongsToMany(Contract::class);
}
Room.php

public function contracts(): BelongsToMany
{
return $this->belongsToMany(Contract::class);
}
Dennis Koch
Dennis Kochā€¢14mo ago
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?
alexanderkroneis
alexanderkroneisOPā€¢14mo ago
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.
No description
alexanderkroneis
alexanderkroneisOPā€¢14mo ago
No description
alexanderkroneis
alexanderkroneisOPā€¢14mo ago
No description
Dennis Koch
Dennis Kochā€¢14mo ago
Can you share the resource form?
alexanderkroneis
alexanderkroneisOPā€¢14mo ago
All of it? It's pretty long
alexanderkroneis
alexanderkroneisOPā€¢14mo ago
Gist
ContractResource.php
GitHub Gist: instantly share code, notes, and snippets.
alexanderkroneis
alexanderkroneisOPā€¢14mo ago
Line 115 - 122
Dennis Koch
Dennis Kochā€¢14mo ago
You are using the same relation 2 times: rooms and hotel-rooms Ah, it's conditional
alexanderkroneis
alexanderkroneisOPā€¢14mo ago
Yes, 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.
Dennis Koch
Dennis Kochā€¢14mo ago
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.
alexanderkroneis
alexanderkroneisOPā€¢14mo ago
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?
Dennis Koch
Dennis Kochā€¢14mo ago
Oh it is hidden. Thought they are using the same condition. Well, it shouldn't save when it's hidden šŸ¤”
alexanderkroneis
alexanderkroneisOPā€¢14mo ago
But only the Tab is hidden. The component of hotel-rooms itself isn't.
Dennis Koch
Dennis Kochā€¢14mo ago
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.
alexanderkroneis
alexanderkroneisOPā€¢14mo ago
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. :/
Dennis Koch
Dennis Kochā€¢14mo ago
Can you isolate this more? Just required fields and the rooms select?
alexanderkroneis
alexanderkroneisOPā€¢14mo ago
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. šŸ’€
Dennis Koch
Dennis Kochā€¢14mo ago
You mean, hotel-rooms? Can't find price-rooms
alexanderkroneis
alexanderkroneisOPā€¢14mo ago
Yes, I renamed that to price-rooms so it's clearer. šŸ˜„
Dennis Koch
Dennis Kochā€¢14mo ago
I think the issue is that Repeater is for HasMany not BelongsToMany
alexanderkroneis
alexanderkroneisOPā€¢14mo ago
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. šŸ˜„
Dennis Koch
Dennis Kochā€¢14mo ago
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
alexanderkroneis
alexanderkroneisOPā€¢14mo ago
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 šŸ”„
Dennis Koch
Dennis Kochā€¢14mo ago
I don't think this needs a PR. That doesn't sound like something that should work out of the box.
alexanderkroneis
alexanderkroneisOPā€¢14mo ago
One last question about this: Can I enforce a refresh after attaching a record via a relation manager?
Dennis Koch
Dennis Kochā€¢14mo ago
Should be possible to trigger a refresh via an emit and a listener on the page that executes $refresh
alexanderkroneis
alexanderkroneisOPā€¢14mo ago
Merci
Want results from more Discord servers?
Add your server