1

I am trying to make a package for Laravel.

I'm getting an issue when i use Auth::user() in controller in package folder it won't return the currently logged in user, it will only return null

When I put Auth::user() in a controller at App\Http\Controllers I do receive the currently logged in user.

enter image description here

Please help me regarding this.

Sid Heart
  • 743
  • 3
  • 14
  • 38
  • You try to get it in the controller construct ? – Maraboc Jul 31 '18 at 10:19
  • Yes 'public function __construct() { $this->middleware(['admin', 'auth']); }' – Sid Heart Jul 31 '18 at 10:20
  • What route hits the controller function you're printing Auth::user() on, is it namespaced correctly? Are you including your `Use` case for Auth Facade at the top of your file, this is difficult to give you an answer without you posting your routes that hit this admin file and the admin controller itself. Please update your question with code. –  Jul 31 '18 at 10:47

1 Answers1

0

You have to do it like this :

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

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

For more infos see this answer

Maraboc
  • 10,550
  • 3
  • 37
  • 48
  • Nope Still Same return null – Sid Heart Jul 31 '18 at 10:27
  • are you authenticated ? – Maraboc Jul 31 '18 at 10:28
  • yes i am pretty sure i am authenticated because when i use in HomeController.php try Auth::user() returning me full details of current user but when i trying to use another folder like package/admin/src/http/controllers/AdminController.php it won't work maybe session not continue in this file – Sid Heart Jul 31 '18 at 10:31
  • What about the import is it like this `use Illuminate\Support\Facades\Auth;` ? – Maraboc Jul 31 '18 at 10:39
  • already Added use Illuminate\Support\Facades\Auth; and use Auth; both tried – Sid Heart Jul 31 '18 at 10:41
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/177081/discussion-between-maraboc-and-sid-heart). – Maraboc Jul 31 '18 at 11:11
  • did this problem solved or not? I am facing the same. I am trying to make package and use Auth::user() inside it but shows null value. – Lal Kumar Rai Mar 16 '20 at 06:24