
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] => [email protected] [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
- Laravel get all records with pagination
- Display option of select as selected with blade directive Laravel
- How to remove package from laravel
- In order to use the Auth::routes() method, please install the laravel/ui package
- Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails
- Remove array keys and values if it does not exist in other array in Laravel
- Drop foreign key column in Laravel using migration
- How to create laravel project using composer
- Send post data from controller to view
- Laravel get single row by id
- Get last week data in Laravel
- How to increment column value of table in Laravel
- How to get all route list
- How to check data inserted or deleted in pivot after toggle method
- Update last created record in Laravel
- Property [user] does not exist on this collection instance
- Pass value from controller to model in laravel
- How to check query string exists or not in laravel blade
- How to get last year records count with month wise in Laravel
- Remove public from url in laravel project
- How to remove P tag from CkEditor in Laravel?
- How to validate form input data in laravel
- How to create event and listener in laravel ?
- Laravel upload file with original file name
- Cast Array to an Object in Controller and then pass to view in laravel