Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Allviinn's avatar

Multiple polymorphic xxxxable_type fields in table

Hi every one ! I'm building my own user / roles system and I've run intro a problem. I have the following tables :

  • users id, name, etc...

  • roles id, slug, tilte

  • role_user (pivot table) user_id, role_id

This is very basic. Next I have my permission table. Actually I have multiple permissions table:

  • permission_groups id, slug, title

  • permissions id, permission_group_id, slug, title

  • permission_functions id, permission_id, slug title

So I want to be able to assign, many to many roles - permissions, and many to many user-permissions. I was thinking on creating 2 polymorphic tables.

First would be :

  • userable user_id, userable_type ( model permission or model permission_ group or model permission_functon ), and a userable_id

  • rolable role_id, rolable_type ( model permission or model permission_ group or model permission_functon ), and a rolable_id.

So my question is, instead of creating 2 polymorphic tables (userable and rolable), is there a way to have a multiple polymoprhic fields i, a single table ? I would like to have a table that looks like :

  • assignable assignable_type_1 ( roles or users models), assignable_id_1 assignable_type_2 (permission group, permissions or permission funtcions) assignable_type_id_2

This is something of course possible if doing it from scratch, nut i was wondering if Laravel's polymorphic system can handle this ? I'm working on the version 8 of Laravel. Thanks in advance and happy code !

0 likes
2 replies
siangboon's avatar

I'm a bit confused.... just wonder why not user has many roles, and a role have many permissions?? so your user just need to bind with roles... and the role have it binding with permissions...

Allviinn's avatar

Yes i could do that, but i would like to be able to create some exceptions. A user may have multiple roles bound to multiple permissions, but i want to be able to give a user an exceptional and single permission without having to create a role for that single permission.

I just wonder if Laravel's native polymorphic system can handle this or not. Based on that I'll choose if I do in the end.

Please or to participate in this conversation.