vendredi 9 mars 2018

Laravel 5: Auth::id() returns NULL in AppServiceProvider.php

I am trying to set a global variable in the AppServiceProvider.php file in Laravel 5. However, I cannot query a database which uses the currently logged in user ID. The Auth::id() returns null.

here is my code;

<?php

namespace App\Providers;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\ServiceProvider;


class AppServiceProvider extends ServiceProvider
{

    public function boot()
    {
        $cnv_count = Conversation::where('receiver_id', Auth::id())->count(); // This Auth::id() here returns null

        view()->share(['cnv_count' => $cnv_count]);
    }


    public function register()
    {

    }
}

It doesn't return any error but when I try printing the the Auth::id() in the AppServiceProvider.php file, it always returns NULL.

what am I doing wrong here?

Appreciate the help guys.



from Newest questions tagged laravel-5 - Stack Overflow http://ift.tt/2GbYVzG
via IFTTT

Aucun commentaire:

Enregistrer un commentaire