
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
- Recursive function example code PHP Laravel
- Laravel migration add foreign key to existing table
- Non-static method App\Http\Helper::myFunction() should not be called statically
- Delete all related comments on deleting a post in Laravel
- How to use bootstrap pagination in laravel 8
- Laravel 9 pagination with search filter
- Create record with unique slug in laravel
- Display first n record from collection in laravel view
- How to access the nth object from Laravel collection object ?
- Page loader in laravel
- Return redirect laravel not working
- How to get path from current URL in Laravel
- Display message with session flash using bootstrap alert class in laravel
- How to check find method executed successfully in laravel
- Laravel route redirect not working
- How to upload image in laravel 8
- How to call controller function from view in Laravel
- Comment .env file in laravel
- Php artisan make model, factory, migration and controller in single command
- How to get query string value in laravel
- Fatal error: Uncaught Error: Class "Illuminate\Foundation\Application" not found
- How to create pivot table in laravel using migration
- Connection could not be established with host smtp.gmail.com :stream_socket_client(): unable to connect to tcp://smtp.gmail.com:587 (Connection refused)"
- There are no commands defined in the "route:" namespace
- How to add foreign key in laravel using migration