Laravel 5.3 auth check in constructor returning false

docs

you can’t access the session or authenticated user in your
controller’s constructor because the middleware has not run yet.

As an alternative, you may define a Closure based middleware directly
in your controller’s constructor. Before using this feature, make sure
that your application is running Laravel 5.3.4 or above:

class ProjectController extends Controller
{
    /**
     * All of the current user's projects.
     */
    protected $projects;

    /**
     * Create a new controller instance.
     *
     * @return void
     */
    public function __construct()
    {
        $this->middleware(function ($request, $next) {
            $this->projects = Auth::user()->projects;

            return $next($request);
        });
    }
}

Leave a Comment