6

I have Laravel 5.4 Base Controller which should share along children Controllers some common data depending on current Authenticated user.

I was Trying to get it like

public function __construct(ValidationFactory $validation)
{
    $this->middleware(array('auth', 'lockscreen'));
    var_dump(\Auth::user());
    die;  
}

this do not works.

8
  • 1
    Be more specific. What exactly doesn't work? Commented Jun 30, 2017 at 15:51
  • this will return null, as I understand mainly because of middleware Commented Jun 30, 2017 at 15:53
  • var_dump(\Auth::user(); misses a ) Commented Jun 30, 2017 at 16:29
  • this is a typo in here, otherwise would dump some error and not null Commented Jun 30, 2017 at 16:33
  • 2
    @fefe That's a change introduced in 5.3. The recommended workaround is to use a closure middleware (see docs for more info). Commented Jun 30, 2017 at 16:35

1 Answer 1

4
private $userId;

public function __construct()
{
   $this->middleware(function ($request, $next) {
        $this->userId = Auth::user()->id;
        return $next($request);
    });
}
Sign up to request clarification or add additional context in comments.

1 Comment

I tried using this. This does not work trying to access the variable outside middleware definition returns null.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.