Replies: 6 comments 5 replies
-
If you're open to it, I'd be happy to help make this package more flexible to support both UUID and bigint-based models out-of-the-box. Whether through a pull request or contributing on specific parts, feel free to reach out — I'd be glad to collaborate and ensure smooth support for both ID types in future versions. |
Beta Was this translation helpful? Give feedback.
-
Beta Was this translation helpful? Give feedback.
-
I'd welcome a PR to review your proposed approach. It'd be easier to see the big picture with actual working code. Presently it's pretty simple to flip to UUIDs with a few changes where noted in the documentation, and it's a one-time thing. I'm a bit reluctant to use |
Beta Was this translation helpful? Give feedback.
-
Also of interest is the idea of using autoincrementing bigInt for primary key but adding a unique uuid column for lookups and public-facing use, where we use the int internally, but for all json/browser/form data the uuid is what's giving/selectable/etc. But that's probably a much larger rearchitecting project which affects userland code in big ways. But if it were as simple as adding a uuid field(column) to the roles/perms models, instead of changing all the existing tables and pivots, that may be an even more performant solution in the end. |
Beta Was this translation helpful? Give feedback.
-
First, I think I'd like to see us create an And then once that's in place, iterate for this present discussion by adding the opportunity to select INT vs UUID during installation, and apply the relevant migration/model changes automatically. |
Beta Was this translation helpful? Give feedback.
-
Hi! First of all, thanks for sharing your thoughts with me — it looks like we’re heading into an interesting and technical discussion. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Support UUID and Bigint in spatie/laravel-permission
model_id
🐛 Problem Overview
When using
spatie/laravel-permission
in projects with UUID-based primary keys (such asusers.id
being auuid
), a type mismatch error occurs due to the default database migration assumingmodel_id
is anunsignedBigInteger
.❌ Example Error
This happens because the default migrations define
model_id
(viamodel_morph_key
) as:This causes a fatal mismatch when UUIDs are used.
✅ Recommended Solution
Change the column type for
model_morph_key
(model_id
) tostring
instead ofunsignedBigInteger
.✅ Updated Migration Snippet
Replace:
With:
This allows UUIDs like
01984de3-23bb-7189-9ff3-c4a343bebb33
and also integer-based IDs like1
or42
.💡 Why This Matters
This allows
spatie/laravel-permission
to:🧩 Alternative Options
If you're using only UUIDs, you could define the column as
uuid
instead:However, this won't support
bigint
IDs, sostring
is the most flexible option.🙏 Final Note
Thanks again for the amazing package. We hope this improvement can be considered to support more Laravel use cases out-of-the-box.
@tikrack
Beta Was this translation helpful? Give feedback.
All reactions