Monday, March 20, 2017

Whats the best way to store user role permissions in Laravel 5?

I'm building a web app in Laravel 5.2. I'm relatively new to Laravel, and want to follow best practices. I have a table called Roles that have several named user roles (i.e.: admin, editor, etc). I want the admin to be able to edit the permissions for these roles and create new ones. What would be the best way to store the permissions?

  1. Use an integer field with each bit representing one permission? (I think this would quickly get out of hand)
  2. Use a pivot table and a many-many connection to permissions?
  3. Use a string field and just serialize the chosen privileges?

New privileges are likely to be added in the future, and my goal is to be able to easily determine if a user has a certain role or not. I.e.: $user->roles->hasAdmin() or something simirar.



via Moha

Advertisement