Creating relationship, changing query? (spatie teams/multi-tenancy)

Hi there, I have the following situation: - filament clinic panel with multi- tenancy on one database - spatie permissions with teams activated + shield - every tenant (clinic) can have users with different roles (with different rights) - so the same user can have all rights in clinic 1 and basic rights in clinic 2 So far so good, everything above is working. Now i want to give users with a high role the ability to give other users a role. I made the following (see screenshot) and this ALMOST works, but when saving i get the following error: SQLSTATE[HY000]: General error: 1364 Field 'clinic_id' doesn't have a default value INSERT INTO model_has_roles (model_id, model_type, role_id) VALUES (2, USER, 3) So for some reason the clinic_id is not set, although it should be. I tried a lot of things with mutateFormDataBeforeSave() etc, but I just can't get it to work. Anyone that knows how to do this? Thanks a million if you already read this far. 🥲
No description
6 Replies
Sjoerd24
Sjoerd245mo ago
hate to do this, but anyone a solution? Is there any method to "hijack" the query and insert another variable like team_id (or clinic_id)?
Sjoerd24
Sjoerd245mo ago
Ok I got a little bit further, I think (with this helpfull post of Dan: https://github.com/filamentphp/filament/issues/10670) that I should do it with a function like this: ->saveRelationshipsUsing(function (Model $model, array $data) { // using default setting of spatie should work (auto set tenant id) $model->user->roles()->sync($data['user']['roles']); }) But this only gives errors because I cant load $data in the closure. 😭 Anyone can help me how to add an extra field in the updating query?
GitHub
Issues · filamentphp/filament
A collection of beautiful full-stack components for Laravel. The perfect starting point for your next app. Using Livewire, Alpine.js and Tailwind CSS. - Issues · filamentphp/filament
toeknee
toeknee5mo ago
So that's a spatie setting you are wanting to use by the looks of it right? And you just want to pass in the team_id. I do this too, but in a different way I built out a Teams selection then a tab for the roles selection and assigned them all that way
Sjoerd24
Sjoerd245mo ago
Yeah the problem is that the role assigning is missing a variable. I should be able to "add" just a variable right?
toeknee
toeknee5mo ago
Do dd on $data what do you have? I doubt you can dot out the array data as it's an arrry and not dot syntaxed here.
Sjoerd24
Sjoerd245mo ago
It gives an error when i dd data, (within this function as i mentioned before).
Want results from more Discord servers?
Add your server
More Posts