saqueib
665
5
Laravel

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

Posted 3 years ago by saqueib

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

Please sign in or create an account to participate in this conversation.

Reply to

Use Markdown with GitHub-flavored code blocks.