More Resources to Learn
I will admit: this course was rewritten three times before publishing. At first, I planned it to be longer, covering roles/permissions in other Laravel-related systems.
But then I realized we had already covered those in other courses, so I decided not to duplicate those lessons here.
Here are the links:
- Roles/permissions in Filament 3
- Roles/permissions in Vue Inertia + Laravel
- Roles/permissions in Vue without Inertia
- Roles/permissions in React (older lesson from 2022)
Also, I like this course because it covers the main things you need to know without spending long hours repeating the same things. But if you feel that some cases of roles/permissions are not covered in this course, feel free to add comments, and I may add them in the future.
hi povilas. always great what you show. do you have a example with jetstream team and spatie roles and permissions. admin, manager, employee. many thanks
Hi Sahrim, no unfortunately I don't have that specific example. In general, Jetstream seems to have popularity only when it was released in Laravel 8, so I shot many videos about it on Youtube.
But since then, only a few people have questions about it, seems like the usage and popularity dropped very significantly, so I stopped creating content about it.
Thanks Povilas. what do you recommend to create a multi tenancy? package or not. How would you best do it?
We have a course on multi-tenancy:
https://laraveldaily.com/course/laravel-multi-tenancy?mtm_campaign=search-results-course
But as always - it depends on your project needs.
thanks!
I see how the factories work, by assigning the role after creation of the user type.
But I would like to understand how to extract the different types from the application, doing the opposite of assignment, namely retrieval of user types.
The application is when you assign a resource in Nova or Filament you have to specify a Model. Since the Model is "User" but actually you need Spatie filtering, I get confused.
Thus how would you retieve all user of a type, e.g. doctors? And how would you retrieve a specific doctor?
Let's say you have a page that must only display doctors.
$user->doctors(); ?
Or to check if you're working with a specific doctor, would this work?
$user->hasRole('doctor'); ?
Is there a shortcut?
Would it be legitimate to create a Model, say App\Models\Doctor, use
protected $table = 'users'
, and have some kind of global scope? Or do we just always end up with the User model and other kinds of scopes? Or maybe can we use something like Model Doctor extends User?I'm not sure I understand the question, but if you use the Spatie/laravel-permission package, then yes, you use methods like
$user->hasRole('Doctor')
orUser::role('Doctor')->get()
.Not sure why you need some other shortcuts, those are not short enough for you?
You can see more methods in the docs of Spatie package.
After thinking about this long and hard I realize I made things too complicated. It's difficult to explain why too complicated, but I've seen this on another project now so it's easier to explain.
This other project is simple - just members and admins. The programmers decided the back-end will be Filament, and members will never need to access it. So they did this:
This opens a whole new can of worms because you can't easily reproduce User and all it's Laravel Framework niceties by just creating a new model and using those implements. There is too much "modify the Framework" stuff. For example, MustVerifyEmail is now just broken.
Those programmers didn't even think about roles.
In my case, I partially reached for roles, and also fell into the same trap to create a new model. Except, my model has this:
This only alleviates the pain somewhat but as the application has grown I am finding new bugs that are hard to troubleshoot. The big breakthrough in your teaching is this simple piece of code:
Had I dilgencty been writing tests and if all my factories assigned roles I would not be in this pickle.
I guess next steps is to merge all the different models and hope that I have enough tests so that things don't break completely. Thanks again for your wonderful work and the comment. I think one of your strong points, is well, getting to the point.
EDIT:
This was also useful!