![Logo loader icon](https://w3codegenerator.com/img/logo-f2.png)
Laravel Auth not working in Construct
Laravel Auth not working in Construct
Through many examples, we will learn how to resolve the "Laravel Auth not working in Construct".
How can i get the logged in user details using Auth in the construct method in Laravel. I am not getting the logged in user details after using `Auth::user()` in the construct method of the controller.class HomeController extends Controller { public function __construct() { $this->user = Auth::user(); } public function index(Request $request){ print_r($this->user);
-
Get logged in user details using __construct method in controller Laravel 9
You can get the logged in user details using the construct method in Laravel. You have to call Auth::user() method after auth middleware which checks if the user is authenticated or not in the construct method and after that you can get the details of the logged in user.
app\Http\Controllers\HomeController.php
use Auth; class HomeController extends Controller { public function __construct() { $this->middleware('auth'); $this->middleware(function ($request, $next) { $this->user = Auth::user(); return $next($request); }); } public function index(){ print_r($this->user);
Output
Array ( [id] => 15 [name] => samar [email] => example@gmail.com [email_verified_at] => [username] => samar [created_at] => 2022-03-08T08:26:50.000000Z [updated_at] => 2022-03-08T08:26:50.000000Z )
If you like what you are reading, please consider buying us a coffee ( or 2 ) as a token of appreciation.
Don't forget to share this article! Help us spread the word by clicking the share button below.
We appreciate your support and are committed to providing you valuable and informative content.
We are thankful for your never ending support.
Random Code Snippet Queries: Laravel
- How to get images from AWS s3 and display in Laravel blade
- Generate random string lowercase in Laravel
- The use statement with non-compound name 'DB' has no effect
- Generate unique username in Laravel
- Extra Filter Query on Relationships in Laravel
- Call to a member function pluck() on null
- How to create static page in Laravel
- Laravel API response format
- Property [user] does not exist on this collection instance
- File_put_contents(/var/www/html/w3code/storage/framework/sessions/CXwN3EXKxERD6jgy3rADcaAAbAx8FRKih2JK7UO9): Failed to open stream: Permission denied
- How to use or operator in laravel
- How to print form data in laravel
- Count all and get 10 records after where condition in laravel
- Recursive function example code PHP Laravel
- Get 30 days older records from table in laravel
- Method Illuminate\Http\Request::validated does not exist
- Get current month records in laravel 7/8
- Link storage folder in laravel 8
- Multiple Level eager loading in Laravel
- Composer\Exception\NoSslException
- Laravel 7 login error message not showing
- How to get id of next record in laravel
- InRandomOrder() method with example in laravel
- How to display HTML tags In Laravel blade
- Get duplicate records in laravel