Nice tutorial, very clear and understandable. Что думаете на счет того, чтобы вынести все эти строки в константы и использовать уже их а не писать везде строки?
Можно и в константы. Строки по сути не супер длинные и их ассортимент не большой, можно и так печатать. Но в IDE с константами будет легче работать - меньше шансов опечататься.
Whats the differencia of using middlewares in the controller rather than Gates like in the video ? I mean, the first protects de routes, and the other only provides if the user has authorization, right ?
I would like to ask how it exactly works: How is the role connected with user table ? In example: I have already existing table users with all users, do I have to create any column which has to be connected with table "roles" from the spatie package, or its somehow directly connected with roles table just thanks to that trait line code in model ?
@@LaravelDaily oh thank you for fast answer, I am glad that I dont have to create anything new in my already existing database and I just have to use that Trait line in my model there :)
Not sure if caching is a good choice here, because what if roles/permissions have changed since last request of the user? I prefer to get the permissions in live mode, and I'm ok with a few quick DB queries for it.
@@LaravelDaily Good point I think Spatie doesn't cache all things (3 Queries get to run on each page), but for the rest, Spatie permission package automatically refreshes the cached data if you change any permission. Reference: github.com/spatie/laravel-permission/blob/master/src/Traits/RefreshesPermissionCache.php
Thanks a lot Povilas. I have a problem with spatie permission in resource routes, I can't define each route with specific permission do you please have any idea how to set the permission for resource routes in web.php NB : i know i can Defined them in controller constructor but i'm preferring to find a way in web.php Thank you in advance
Have you solved the problem? I'm thinking of using the package and concerned about resource routes too. But isn't it a good idea to use check roles in web.php and check the specific permissions in the method of that resource controller. Let's say we have 'user', 'author' and 'admin' roles. Authors and admins can see the posts in the admin panel but only admins can delete. I think we limit that with 'author' and 'admin' in the web.php so users can't get in the admin/posts page. And then we check the permission of 'post_edit' in the edit and create methods. It's just a quick idea. Please leave your comments about that.
There's Laratrust and Bouncer, which work in a very similar way. Not sure if my video would add something useful more than this video about Spatie permission as example, and more than official docs of those packages.
Great, I have integrated Jetstream with spatie, all right but only one problem with roles of spatie not working when show the profile page of Jetstream livewire. I have created two roles: Admin and User, and I have created too Roleseeder with permission and roles for all page and profile, but profile page not working with spatie and "User" role show error 404,what's going on? Do you have one idea? Thanks
@@LaravelDaily I have already solved the problem, it was specifically generating jetstream in the following route, you can change the url: your-project\vendor\laravel\jetstream outes\livewire.php
I went through the docs but I have a question Let's say we have ability to create schools And we can add admin to that school Now how do you check if an admin is trying to change something that belongs to another school I'll probably make a function like Public static function($school_id){ return auth()->user()->school === $school_id; } Is there any way to achieve this
The Spatie package also contains Teams as well as Roles, so a User can belong to a Team. So, create a Team for the school and assign users to that Team/School.
hello sir thanks for this video i want to add tdd in my project with spatie but when i want to add code and test i get this error Class "Spatie\Permission\PermissionRegistrar" not found did you have any solution or video for this problem . how i gave role to my user in tdd.