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

MB's avatar
Level 2

Using Blade Auth with NovaServiceProvider?

Hi,

I normally use the following in Blade, to show Nova-button, if user is allowed to access:

@if(Auth::user()->email == '[email protected]')
...

How can I modify above code, to match against NovaServiceProvider file? Or is there a better way to handle it?

(I define what e-mails are allowed to access Nova, using the NovaServiceProvider file)

    protected function gate()
    {
        Gate::define('viewNova', function ($user) {
            return in_array($user->email, [
                '[email protected]',
                '[email protected]',
            ]);
        });
    }
0 likes
4 replies
drewdan's avatar

If you have defined the gate, you should be able to use the blade helper function

@can('viewNova')
    <a href="#">Foo</a>
@endcan
MB's avatar
Level 2

@drewdan Thanks for the reply.

I have this in my NovaServiceProvider file:

protected function gate()
    {
        Gate::define('viewNova', function ($user) {
            return in_array($user->email, [
                '[email protected]',
                '[email protected]',
            ]);
        });
    }

That's all I have to do? This does not work in blade though:

@can('viewNova')
    <a href="#">Foo</a>
@endcan

No errors. I can access Nova just fine (after login). I need to include the Gate in Blade somehow (this is outside Nova)?

drewdan's avatar
drewdan
Best Answer
Level 15

I decided to test this out, as close as I could, without having nova available:

In my AppServiceProvider:

<?php

namespace App\Providers;

use Illuminate\Pagination\Paginator;
use Illuminate\Support\Facades\Gate;
use Illuminate\Support\ServiceProvider;

class AppServiceProvider extends ServiceProvider {

	/**
	 * Register any application services.
	 *
	 * @return void
	 */
	public function register() {
		if ($this->app->environment('local')) {
			$this->app->register(TelescopeServiceProvider::class);
		}


	}

	/**
	 * Bootstrap any application services.
	 *
	 * @return void
	 */
	public function boot() {
		Paginator::useBootstrap();

		Gate::define('viewNova', function ($user) {
            return in_array($user->email, [
                '[email protected]',
            ]);
        });
	}
}

Then added this to a blade file:

<div class="container-fluid">
		@can('viewNova')
			<h1>Can view nova</h1>
		@endcan

And "can view nova" appeared in the blade, as expected.

So, I can only assume that this gate is not being applied properly, or potentially Nova does not register the service provider in local environments.

1 like

Please or to participate in this conversation.