wire-elements / spotlight

Livewire component that brings Spotlight/Alfred-like functionality to your Laravel application.
MIT License
925 stars 72 forks source link

Can't access auth()->user() in AppServiceProvider boot() #16

Closed adddz closed 3 years ago

adddz commented 3 years ago

The documentation mentions that you can register the command in the boot() method of the AppServiceProvider file.

use \App\SpotlightCommands\CreateUser;

class AppServiceProvider extends ServiceProvider
{
    public function boot()
    {
        Spotlight::registerCommand(CreateUser::class);

        // You can also register commands conditionally
        Spotlight::registerCommandIf($user->isAdmin(), CreateUser::class);
        Spotlight::registerCommandUnless($user->isSuspended(), CreateUser::class);
    }

}

This is not working because $user or auth()->user() cannot be accessed during the boot() method.

Any ideas on how to conditionally register commands based on the user?

PhiloNL commented 3 years ago

Hi @adddz,

That's correct, there's a PR to resolve this and will be available soon https://github.com/livewire-ui/spotlight/pull/15

PhiloNL commented 3 years ago

Hi @adddz You can now use the shouldBeShown method to do this:

adddz commented 3 years ago

Thank you so much! Could you please port this feature to the PHP 7.4 version as well, please?