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
- Laravel 9 pagination with search filter
- How to get user information using hootlex/laravel-friendships package in laravel
- How to upload local Laravel project to server ?
- How to display a specific word from a string in laravel
- How to return error message from controller to view in laravel
- Route not defined in Laravel
- Display success message in laravel
- Laravel form request validation
- How to display order by null last in laravel
- How to change default timestamp fields name in Laravel
- Use of undefined constant laravel
- How to get records in random order in laravel
- Convert multidimensional array to single array in Laravel
- Call to undefined method Illuminate\Support\Facades\Request::all()
- The Pusher library requires the PHP cURL module. Please ensure it is installed
- How to get last record from object collection in laravel
- How to create controller in laravel
- How to check data inserted or deleted in pivot after toggle method
- Run artisan command to generate key in laravel
- Method Illuminate\Events\Dispatcher::fire does not exist
- Skip first n record and display rest records in laravel view
- Conditional validation in laravel
- How to call model in blade laravel
- SQLSTATE[42000]: Syntax error or access violation: 1091 Can't DROP 'posts_user_id_foreign'; check that column/key exists
- Split an Eloquent Collection by half in Laravel