6

I am using laravel 5.7, but i can't get current user id in __construct().

I also tried Auth:id(), but it also not working.

How to get current user id in constructor?

use Illuminate\Support\Facades\Auth;

class TestController extends Controller
{

    public $id;

    public function __construct()
    {
        $this->middleware('auth');
        $this->middleware(function ($request, $next) {
            $this->id = Auth::user()->id;
            return $next($request);
        });
        dd($this->id);
    }
}

Current output is null.

WO Developers
  • 325
  • 2
  • 5
  • 13
  • 2
    In previous versions of Laravel, you could access session variables or the authenticated user in your controller's constructor. This was never intended to be an explicit feature of the framework. In Laravel 5.3, you can't access the session or authenticated user in your controller's constructor because the middleware has not run yet. More info: https://stackoverflow.com/questions/39175252/cant-call-authuser-on-controllers-constructor – AgeValed Mar 19 '19 at 14:29
  • you can't do that, try with the example in "Session In The Constructor" -> https://laravel.com/docs/5.3/upgrade#upgrade-5.3.0 – AgeValed Mar 19 '19 at 14:35
  • First solution is same as above and it;s not working. For second solution, i want value only in constructor. – WO Developers Mar 19 '19 at 14:35
  • I see that, Please check my code, it is same as solution code. use Illuminate\Support\Facades\Auth; use Illuminate\Http\Request; class HomeController extends Controller { protected $projects; public function __construct() { $this->middleware(function ($request, $next) { $this->projects = Auth::user(); return $next($request); }); dd($this->projects); } } But it doesn't working. – WO Developers Mar 19 '19 at 14:38
  • 1
    sadly you can't view the data on __construct, if you do dd($this->projects); into another method you will see the content. – AgeValed Mar 19 '19 at 14:49

3 Answers3

9

You can only access the session in the closure. Just refactor your code to this:

public function __construct()
{
    $this->middleware('auth');
    $this->middleware(function ($request, $next) {
        $this->id = Auth::user()->id;
        dd($this->id);

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

You can now use the value $this->id in your controller methods.

In the example in your question, after you've set the value $this->id, you continue with the request. Since you try to access $this->id outside of the scope of the closure, it still is null in the datadump.

piscator
  • 8,028
  • 5
  • 23
  • 32
6

After return you will not go to next statement that's why it is not print.

If you want to use this in view then no need to pass in view you can simply access logged user id like this

{{Auth->user->id}}

if you wan to use this in controller make sure you are logged in.

Sometime session expired then you will not get user id

use Illuminate\Support\Facades\Auth;

class TestController extends Controller
{

    public $id;

    public function __construct()
    {
        $this->middleware('auth');
        $this->middleware(function ($request, $next) {
        $this->id = Auth::user()->id;
         dd($this->id);
        return $next($request);
       });

    }
  }
Gurpal singh
  • 1,511
  • 1
  • 14
  • 27
0

The easiest solution is to create a middleware and call it later in the constructor.

php artisan make:middleware FoobarMiddleware

I recommend putting an alias in Kernel.php

protected $routeMiddleware = [
...
'foobar' => \App\Http\Middleware\FoobarMiddleware::class, 
]

Constructor:

public function __construct()
{
    $this->middleware('auth');
    $this->middleware('foobar');
}

I recommend changing the focus of how you are creating everything

Victor Nuñez
  • 119
  • 2
  • 12