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

PyaeSone's avatar

Is Session Helper allowed to use within Gate in Laravel? I am always getting false from @can Laravel Gate?

I want to check whether user is allow to use certain action or not. If user is not allowed, II want to hide action buttons from users. So, I put permission in session helper. I defined in gate. I want to check in blade with @can.

This is why I am defined in my AuthServiceProvider.php

<?php

namespace App\Providers;

use Illuminate\Support\Facades\Gate;
use Illuminate\Foundation\Support\Providers\AuthServiceProvider as ServiceProvider;

class AuthServiceProvider extends ServiceProvider
{
    /**
     * Register any authentication / authorization services.
     *
     * @return void
     */
    public function boot()
    {
        $this->registerPolicies();

        Gate::define('add-inventory', function () {
            return session('add_inventory') === 1;
        });
    }
}

In my blade,

@can('add-inventory')
    <li role="presentation">
        <a role="menuitem" tabindex="-1" href="addinventory/{{ $product->products_id }}">
            {{ trans('labels.addinventory') }}
        </a>
    </li>
    <li role="presentation" class="divider"></li>
@endcan

But @can('add-inventory') is always returning false. Is session allowed to use in Gate? Or should I use helper function or something else?

0 likes
1 reply
bugsysha's avatar

Or should I use helper function or something else?

session() is global helper function.

No one can stop you from using something were ever you want.

1 like

Please or to participate in this conversation.