mgolshan's avatar

Auth::user() returns null in laravel 5.2

have searched and found various results. I tried them. But, mine still is not working.

Auth:user() works in the controller. but not in the Model, returns null.

the code is:

public function scopeOwned($query){
        $query->where('user_id', '=' , Auth::user()->id);
    }

I tried dd(Auth::user()) as well. it returns null.

Any Idea?

Also, I don't how can I make the code blocks pretty in this forum ;)

0 likes
10 replies
Snapey's avatar

Pretty code blocks are surrounded by three backticks`

public function scopeOwned($query)
{
    $query->where('user_id', '=' , Auth::user()->id);
 }

When you call this, are you in a route covered by 'web' middleware?

What Laravel version?

mgolshan's avatar

Thank you. Version 5.2

routes.php :

Route::group(['middleware' => 'web'], function () {
    Route::auth();
    Route::get('/', function () {
        return view('welcome');
    });

    Route::get('/home', 'HomeController@index');
    Route::resource('pages','PagesController');
    Route::get('tags/{tags}', 'TagsController@show');

    Route::resource('cities','CitiesController');
    Route::get('cities/countryStates/{id}','CitiesController@countryStates');

    Route::resource('commodityCategories','CommodityCategoriesController');
    Route::resource('commodities','CommoditiesController');

    Route::resource('companies','CompaniesController');

    Route::resource('packages','PackagesController');

});
Snapey's avatar

possibly daft question... definitely logged in?

mgolshan's avatar

Yes. I am logged in. It detects user info in the controller. But has problem in the model.

thomaskim's avatar

You sure it's not because you aren't returning anything? You should be returning the query instance.

public function scopeOwned($query){
    return $query->where('user_id', '=' , Auth::id());
}
mgolshan's avatar

@thmoskim , No, it's not because of that. I wrote dd(Auth::user()) before return. It shows null.

mgolshan's avatar

This is the code that I wrote in RouteServiceProvider.php:

$router->bind('companies', function($id){
            return Company::owned()->findOrFail($id);
        });

This is the scope in the Company.php model:

    public function scopeOwned($query){
//        dd(Auth::user());
        $query->where('user_id', '=' , Auth::user()->id);
    }

So, based on Model binding, it should work in case of passing id (like edit/delete)

thomaskim's avatar
Level 41

@mgolshan You don't have access to the authenticated user from the service provider because the middlewares that start your session kick in later in the lifecycle.

What you can do is move the middleware to your global stack middleware, and then I believe you will have access to the authenticated user. In your app/Http/Kernel.php, move everything inside the web middleware group to the global HTTP middleware stack. For example:

    protected $middleware = [
        \Illuminate\Foundation\Http\Middleware\CheckForMaintenanceMode::class,
        \App\Http\Middleware\EncryptCookies::class,
        \Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse::class,
        \Illuminate\Session\Middleware\StartSession::class,
        \Illuminate\View\Middleware\ShareErrorsFromSession::class,
        \App\Http\Middleware\VerifyCsrfToken::class
    ];

If you do this, you no longer need the web middleware, and you will have access to the authenticated user in your service provider.

1 like

Please or to participate in this conversation.