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

saqueib's avatar

Auth::user() is returning null in Notify Class, but user is logged in

I have an strange problem, I am not able to get the Auth::user() returned the logged user, its working in controller.

use Illuminate\Support\Facades\Auth;

class Notify
{

    public function comment($comment)
    {
        //User of Post
        $post_user = $comment->post->user('id, username')->first();

        if(Auth::user()->id != $post_user->id) { //Its retuning trying to get property of no-object
            ....
        }
    }

    ....
}

ServiceProvider

use Illuminate\Support\ServiceProvider;

class NotifyServiceProvider extends ServiceProvider {

    /**
     * Bootstrap the application services.
     *
     * @return void
     */
    public function boot()
    {
        //
    }

    /**
     * Register the application services.
     *
     * @return void
     */
    public function register()
    {
        $this->app->bind('notify', 'App\Notify\Notify');
    }

}

Facade

use Illuminate\Support\Facades\Facade;

class Notify extends Facade {

    /**
     * Get the registered name of the component.
     *
     * @return string
     *
     * @throws \RuntimeException
     */
    protected static function getFacadeAccessor()
    {
        return 'notify';
    }
}

EventServiceProvider


class EventServiceProvider extends ServiceProvider {

    /**
     * The event handler mappings for the application.
     *
     * @var array
     */
    protected $listen = [
        'event.name' => [
            'EventListener',
        ],
    ];

    /**
     * Register any other events for your application.
     *
     * @param  \Illuminate\Contracts\Events\Dispatcher  $events
     * @return void
     */
    public function boot(DispatcherContract $events)
    {
        parent::boot($events);

        $notifySubscriber = new NotifyEventHandler();
        \Event::subscribe($notifySubscriber);

        $userSubscriber = new UserEventHandler();
        \Event::subscribe($userSubscriber);
    }

}

I have tried following

  • composer dump-autoload
  • composer update
  • comment out my changes and test
  • tested in other browser

Please help

0 likes
5 replies
bimalshah72's avatar
Level 6

@saqueib

Did you try this? use \Auth - as we do have already Auth facade registered in config/app.config

use \Auth;

class Notify
{

    public function comment($comment)
    {
        //User of Post
        $post_user = $comment->post->user('id, username')->first();

        if(Auth::user()->id != $post_user->id) { //Its retuning trying to get property of no-object
            ....
        }
    }

    ....
}
1 like
saqueib's avatar

Thanks bimalshah72, it worked. I have been stuck on this since yesterday.

What does it mean \Auth with slash?? Is it goes to root namespace and try to load the class, which then triggers Auth Facade since there is alias in config/app.php ?

bimalshah72's avatar

@saqueib

Yes true it starts finding from root namespace, Otherwise, it starts from current namespace, as current laravel has implemented psr-4 namespace

deepu07's avatar

@bimalshah72 @saqueib do you guys have any idea how to find current user info inside of the Config/app.php file? Any help that would be great. Thanks!

Please or to participate in this conversation.