5

I have searched and found various results like these: auth()->user() is null in Laravel 5.2 and Auth::user() returns null

But, mine is still not working.

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

The code is:

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

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

Any Idea?

Community
  • 1
  • 1
Mojtaba
  • 4,852
  • 5
  • 21
  • 38
  • 1
    Are you logged in when you `dd`? – Matt Komarnicki Feb 04 '16 at 00:18
  • Make sure you have `use Illuminate\Support\Facades\Auth;` at the top of your model. – smartrahat Feb 04 '16 at 00:19
  • I have logged in. As I mentioned, it recognizes the user in the controller. But, not in the model. Also, I used the namespace – Mojtaba Feb 04 '16 at 00:23
  • 1
    While it doesn't answer the question, you should pass a $user object into the model. Otherwise if you loaded a user and called the scope, it would still use the authenticated user (if there even is one). – tjbp Feb 04 '16 at 01:45

4 Answers4

6

Thank you guys.

The problem solved here: https://laracasts.com/discuss/channels/laravel/authuser-returns-null-in-laravel-52

I had to add the stack into middleware directly (not in the group).

in /Http/kernel.php:

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
];
Mojtaba
  • 4,852
  • 5
  • 21
  • 38
  • For posterity, could you include the relevant parts of the link which you found useful in your answer? That way, if the link goes down, changes, etc., your answer won't be rendered useless. Many thanks. – Wai Ha Lee Feb 04 '16 at 20:49
  • Wai, You are right. But, I think reading that full discussion can helps more. – Mojtaba Feb 06 '16 at 17:15
  • 1
    Still worked for me in Laravel 9.x. I only moved the middleware with the issue to the web portion to get it to work. – MrMedicine Aug 09 '22 at 13:05
1

I had a similar problem. I was defining a custom Middleware for the API group and Auth was always appearing as null.

I fixed this by loading in the EncryptCookies Middleware before my custom one.

'api' => [
    \App\Http\Middleware\EncryptCookies::class,
    \App\Http\Middleware\VerifyParametersMiddleware::class, // your custom middleware, load after EncryptCookies
    'throttle:60,1',
    'bindings',
],

This change can be made within Kernel.php.

Toby Mellor
  • 8,093
  • 8
  • 34
  • 58
-1

From the same discussion that the accepted answer links to, I found the following solution to work. Edit your /routes/web.php file and surround your existing routes with Route::group('middleware' => 'web']), like so:

Route::group(['middleware' => 'web'], function () {

    Auth::routes();

    // The rest of your routes

});

As far as I understand, this will not have any security or other implications, as it is simply loading the web middleware prior to processing the contained routes. If anyone knows of security or other concerns / ramifications, please post them in the comments on this answer.

Chad
  • 1,531
  • 3
  • 20
  • 46
-2

I have a similar problem with Laravel 5.2, Try to change Auth::user()->id to Auth::id()

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

It's worked for me!