mercredi 28 mars 2018

Laravel and 3+ table pivot

I'm trying to wrap laravel around a relatively basic structure - users with roles in multiple organisations, as in a single user can have many roles in many organisations. This allows a user to have a single set of sign-in credentials with different access depending on the organisation they're acting behalf of.

In MySQL/MariaDB, i would typically represent this with a table having three columns with a unique combination constraint

CREATE TABLE `organisation_role_user` (
  `organisation_id` int(10) unsigned NOT NULL,
  `role_id` int(10) unsigned NOT NULL,
  `user_id` int(10) unsigned NOT NULL,
  UNIQUE KEY `organisation_role_user` 
(`organisation_id`,`role_id`,`user_id`),
  KEY `organisation_role_user_ibfk_2` (`user_id`),
  KEY `organisation_role_user_ibfk_3` (`role_id`),
  CONSTRAINT `organisation_role_user_ibfk_1` FOREIGN KEY 
(`organisation_id`) REFERENCES `organisations` (`id`) ON DELETE CASCADE,
  CONSTRAINT `organisation_role_user_ibfk_2` FOREIGN KEY (`user_id`) REFERENCES `users` (`id`) ON DELETE CASCADE,
  CONSTRAINT `organisation_role_user_ibfk_3` FOREIGN KEY (`role_id`) REFERENCES `roles` (`id`) ON DELETE CASCADE
)

I'm wondering that someone must have used a similar construct in Laravel / Eloquent before me - it's a basic thing.

Is there a way to do this neatly with attach() / detach() / sync() working? There must be something i'm missing?

Thank you for your time.



from Newest questions tagged laravel-5 - Stack Overflow https://ift.tt/2GfPbb7
via IFTTT

Aucun commentaire:

Enregistrer un commentaire