Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

vedoj's avatar
Level 1

Ways to optimize this code of custom Blade guards in AppServiceProviders.php?

I have currently this in my AppServiceProvider.php file:

public function boot()
{
    Blade::if('user', function () {
        $user = Auth::user();
        return auth()->user() && $user->role_id === 1;
    });
    
    Blade::if('editor', function () {
        $user = Auth::user();
        return auth()->user() && $user->role_id === 2;
    });

    Blade::if('manager', function () {
        $user = Auth::user();
        return auth()->user() && $user->role_id === 3;
    });
    
    Blade::if('admin', function () {
        $user = Auth::user();
        return auth()->user() && $user->role_id === 4;
    });
}

How would you optimize this code? It seems very repetitious to me. But I am not sure how Blade:: works so I am not sure how to limit the db calls and stuff.

Thanks for any help in advance.

0 likes
10 replies
Sinnbeck's avatar

You call auth twice? Also add a helper method on the user model

Blade::if('user', function () {
        $user = Auth::user();
        return $user && $user->isUser(); //or ->hasRole('user') 
    });

But none of them should do any database calls. User is already loaded

2 likes
vedoj's avatar
Level 1

@Sinnbeck Thanks, yeah you are right I will use $user that I already get from Auth::user(). Thank you again.

vedoj's avatar
Level 1

@Sinnbeck One more question, could I put $user = Auth::user(); outside of Blade and then call it via $user inside of each Blade? Something like this:

public function boot()
{
    
    $user = Auth::user();    

    Blade::if('user', function () {
        return $user && $user->role_id === 1;
    });
    
    Blade::if('editor', function () {
        return $user && $user->role_id === 2;
    });

    ...

}

or like this?

public function boot()
{
    
    $user = Auth::user();    

    Blade::if('user', function ($user) {
        return $user && $user->role_id === 1;
    });
    
    Blade::if('editor', function ($user) {
        return $user && $user->role_id === 2;
    });

    ...

}

Will this work?

Sinnbeck's avatar

@vedoj you need to pass it into the scope

    Blade::if('user', function () use ($user) {
        return $user && $user->role_id === 1;
    }); 
vedoj's avatar
Level 1

@Sinnbeck Yeah, use, of course, so, which code is more efficient and will do less calls if all four blade guards will be present in some view at once?

Code A:

public function boot()
{
    Blade::if('user', function () {
        $user = Auth::user();
        return $user && $user->role_id === 1;
    });
    
    Blade::if('editor', function () {
        $user = Auth::user();
        return $user && $user->role_id === 2;
    });

    Blade::if('manager', function () {
        $user = Auth::user();
        return $user && $user->role_id === 3;
    });
    
    Blade::if('admin', function () {
        $user = Auth::user();
        return $user && $user->role_id === 4;
    });
}

or

Code B:

public function boot()
{
    
    $user = Auth::user();    

    Blade::if('user', function () use ($user) {
        return $user && $user->role_id === 1;
    });
    
    Blade::if('editor', function () use ($user) {
        return $user && $user->role_id === 2;
    });

    Blade::if('manager', function () use ($user) {
        return $user && $user->role_id === 3;
    });
    
    Blade::if('admin', function () use ($user) {
        return $user && $user->role_id === 4;
    });
}
Sinnbeck's avatar
Sinnbeck
Best Answer
Level 102

@vedoj both do 0 database calls, so I don't think you will find any difference between the two

2 likes
vedoj's avatar
Level 1

@Sinnbeck That would be great, because the code B will be easier to maintain. Thanks again.

1 like
vincent15000's avatar

Auth::user() and auth()->user() are the same.

So you can simplify like this for each if statement.

Blade::if('user', function () {
    $user = Auth::user();
    return $user && $user->role_id === 1;
});

If you really want each different if statement to use @user, @editor, ... it's pretty good.

Instead you can add pass variable to the if statement.

https://laravel.com/docs/9.x/blade#custom-if-statements

And if you have variables like Role::ADMIN, you could for example use it in a unique if statement.

@role(Role::ADMIN)
	...
@endrole
1 like
vedoj's avatar
Level 1

@vincent15000 Thanks.

Just like in my previous question about middleware performance, which you answered as well ;), I would ask you about guards the same.

Will having 20 or 100 Blade if guards slow down the performance of my website? Or only the actual guards I use on each page matter when it comes to performance?

1 like

Please or to participate in this conversation.