Suggest a package or how you can fix your code?
Most of us use this https://spatie.be/docs/laravel-permission/v5/introduction
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
i have created a full roles and permission system without installing package but it doesn't working it works like a simple login can anyone suggest me something. Thanks in advance
Suggest a package or how you can fix your code?
Most of us use this https://spatie.be/docs/laravel-permission/v5/introduction
Here's my code in service provider
namespace App\Providers;
use Illuminate\Support\ServiceProvider; use Illuminate\Support\Facades\Blade; use Illuminate\Support\Facades\Gate; use App\Models\permission;
class PermissionServiceProvider extends ServiceProvider { public function register() { // }
public function boot()
{
try {
permission::get()->map(function ($permission){
Gate::define($permission->slug, function ($user) use ($permission){
return $user->hasPermissionTo($permission);
});
});
} catch (\Exception $e){
report($e);
return false;
}
Blade::directive('role', function ($role) {
return "if(auth()->check() && auth()->user()->hasRole({$role})) :";
});
Blade::directive('endrole', function ($role) {
return "endif;";
});
}
}
i dont know whats happeing but is there any bug in it can you check?
@laksh maybe start by explaining what happens. "Doesn't work" isn't much to go by
@Sinnbeck yeah so i want that if ant other employee try to edit anything will get error but in my case the edit window will open and this will print
if(auth()->check() && auth()->user()->hasRole('manager')) :
but not giving error
@laksh It isnt written as php
Blade::directive('role', function ($role) {
return "<?php if(auth()->check() && auth()->user()->hasRole({$role})) : ?>";
});
Blade::directive('endrole', function ($role) {
return "<?php endif; ?>";
});
@Sinnbeck i changed but it was giving same error
@laksh You said no error? Is it showing the same output? Try php artisan view:clear
@Sinnbeck after that now it's giving an error here's error : syntax error, unexpected 'endif' (T_ENDIF), expecting end of file (View: D:\xampp\htdocs\demo\resources\views\posts\edit.blade.php)
@laksh Show how you are using it
Blade::directive('role', function ($role) {
return "<?php if(auth()->check() && auth()->user()->hasRole({$role})); ?>";
});
Blade::directive('endrole', function ($role) {
return "<?php endif; ?>";
});
}
Maybe try without {}
Blade::directive('role', function ($role) {
return "<?php if(auth()->check() && auth()->user()->hasRole($role)) : ?>";
});
Blade::directive('endrole', function ($role) {
return "<?php endif; ?>";
});
@Sinnbeck it is giving error on endif statement
@laksh Could you show how you use it? In blade that is
@Sinnbeck i am using this with the help of @role
@role('manager')
<div class="container">
<div class="row justify-content-centre">
<div class="col-md-8">
<div class="card">
<div class="card-header">{{ __('Edit Post') }}</div>
<div class="card-body">
@if(session('status'))
<div class="alert alert-success">
{{ session('status') }}
</div>
@endif
<form action="{{ route('update', $post->id) }}" method="post">
@csrf
@method('PUT')
<div class="form-group">
<label for="">Post Tittle</label>
<input type="text" name="tittle" class="form-control" value = "{{ ($post->tittle) }}">
</div>
<div class="form-group">
<label for="">Post Body</label>
<textarea name="body" id="" cols="30" rows="10" class="form-control" >{{ $post->body }}</textarea>
</div>
<button type="submit" class="btn btn-primary">Submit</button>
</form>
</div>
</div>
</div>
</div>
</div>
@laksh Cant say what you are doing wrong. It works for me. Did you remove {} from the directive like i told you?
@Sinnbeck yeah but nothing happened
@laksh Ok Im not sure how to help you then. I copied the blade code into a new blade file with nothing else, and copied over the two directives from my last example, and it worked. Maybe you have something that you arent showing? Does it work if you rename the directives to something else? Like "rolecheck" and "endrolecheck"
@Sinnbeck umm basically i want that if i login from developer id it doesn't show me the edit view but in my case it is showing me the view without error
@laksh So there is no error now? So its working or?
@Sinnbeck yeah there is no error but its not working like how i want to work that
@laksh Ok. I dont know how I can help with that? What is happening and what do you expect?
so your actual issue is writing blades directives, not your permissions?
make sure you clear the view cache each tome you change the blade design
I think the issue is that you can't just pass the role name in as a simple variable. im checking my notes but it's a long time since i bothered creating blade extensions
try this instead
Blade::directive('role', function ($role) {
return "<?php if(auth()->check() && auth()->user()->hasRole('{$role}')); ?>";
});
notice the quotes around the role
clear all views, load the page, then find the file just created in your storage/framework/views folder
open it and check the php that was generated by your blade extension
@Snapey yeah i am clearing the cache everytime i changed the view
@laksh please see my updated answer
@Snapey It works for me with my example from before, without quotes. :)
https://laracasts.com/discuss/channels/laravel/roles-and-permission-doesnt-working?reply=818684
@Sinnbeck does it say 'manager'in the generated code it does it say $role ?
@Snapey It autocatically quotes it :)
This is from the compiled file
<?php if(auth()->check() && auth()->user()->hasRole('manager')) : ?>
@Snapey no it's doing nothing
@laksh What does that mean?
@Sinnbeck yess it automatically quote it
@laksh Can you please spend 2 minutes explaining exactly what is happening and what you expect to happen?
@Sinnbeck basically i want that i made a blog page and on that page a developer can only will create not edit means developer has not permission to edit the blog but in my case the developer can able to edit the blogs which is wrong
@laksh are you sure the developer is not also manager. can a user have more than one role?
you created the gates, but how is permissions assigned to a user?
@laksh What does this give you?
@dd(auth()->user()->hasRole('manager')))
@Sinnbeck it gives me False.
@Snapey yeah am sure that developer is not a manager
you created the gates, but how is permissions assigned to a user?
@Snapey i don't know what do you mean to say how permissions can be assigned
@laksh what gives the user the role of 'Manager' or 'developer'
@Snapey i created two tables into database one is roles and one is permissions and from there the roles and permission were assigned to the manager and developer
@Snapey when i create user manager this code is used by me to assigning permissions.
$manager = new User();
$manager->name = 'manager';
$manager->email = '[email protected]';
$manager->password = Hash::make('4321');
$manager->save();
$manager->role()->attach($manag_role);
$manager->permission()->attach($manag_perm);
@laksh So you have some code somewhere that does hasRole()
@Snapey yeah here it is
public function hasRole( ... $role){
foreach ($role as $roles){
if ($this->role->contains('slug',$roles)){
return true;
}
}
return false;
}
@laksh bit by bit we get the information....
so role is a relationship on the user model, and the code you show is in the User model also but its a one to Many relationship but you have named it singular? Have you tested it even returns any roles?
Can you show your entire User model?
@Snapey the code i show is in HasPermissionTrait file which was created by me so you want to see the whole file in which that code is written which was shown by me?
@laksh So you are sorted then.... all working? If not then you need to provide the code to help diagnose the issue
@Snapey no it wasn't working i mean to say according to me permissions were assigned to users correctly but they don't give results
public function boot() { try { permission::get()->map(function ($permission){ Gate::define($permission->slug, function ($user) use ($permission){ return $user->hasPermissionTo($permission); }); }); } catch (\Exception $e){ report($e); return false; }
Blade::directive('role', function ($role) {
//return @dd(auth()->user()->hasRole('manager'));
if (auth()->guest()){
abort(Response::HTTP_FORBIDDEN);
}else {
return "if(auth()->check() && auth()->user()->hasRole({$role})) ;";
}
});
Blade::directive('endrole', function ($role) {
return "endif;";
});
}
the return response in this code print as it is on my page it won't render can you tell me what should i do for that?
@Snapey you mean to say by using <? php tag in return string
@laksh Blade extensions must return a string containing valid PHP code that will be included in the cached view file and then executed when that cached file is loaded.
It should not evaluate any run time data in the blade extension itself, but only prepare the code that will evaluate run time data later
So as Sinnbeck proposed
Blade::directive('role', function ($role) {
return "<?php if(auth()->check() && auth()->user()->hasRole($role)) : ?>";
});
Blade::directive('endrole', function ($role) {
return "<?php endif; ?>";
});
@Snapey but in this code if part doesn't give any response and endif part gives error on using <?php
@laksh This question has best answer so nothing else to do?
If not, show some errors.
Please or to participate in this conversation.