Reputation: 686
I want to have users with different roles and permissions - for example normal user - readonly, mods - edit/delete, admins - create/delete/edit. By default Laravel provides users model with migration. I read, for this purpose there should be additional tables for roles and permission. But I have no idea what structure this tables should have and what to do next. Are there any good tutorial for beginners for my question or can someone post an easy example?
Upvotes: 1
Views: 1388
Reputation: 2486
You can always use a package like https://github.com/spatie/laravel-permission, or you can create your own, here is the algorithm for you.
1- Create a table/Model called Roles.
2- Set up the relationship to be 1 to many (if you want a user to have multiple permissions) or 1 to 1 (if you want the user to have one permission)
3- create a middleware called RolesMiddlware per example, that gonna check after every route call, if the user has permission depending on the roles he has, if he has the appropriate permission then proceed otherwise return an error.
Upvotes: 3