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?
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 ;)
@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.
Please or to participate in this conversation.