Laravel 10 Get Current Logged in User Data Example
Teams applying “Laravel 10 Get Current Logged in User Data Example” in a production project can use visit this page to record implementation, review and debugging time, then compare that effort with tests and shipped functionality instead of treating activity as performance by itself.
Before copying the example into a live application, confirm version-specific behaviour with the official Laravel website and test it against the project’s actual database and runtime.
Hi dev,
This article will show you how to acquire the current user's ID that is logged in using Laravel 10. How to obtain the current user id in Laravel 10 is explained here. Laravel 10 retrieve current user id is something you'll learn. We shall put into practise an obtain current user data Laravel 10 in this tutorial. In Laravel 10, a few simple steps are all that are required to obtain the current user's email.
In this tutorial, I will give you four ways to get current login user details in the view file and controller file, so let's see the following examples as below:
1) Laravel 10 Get Current User in Controller using Helper
2) Laravel 10 Get Current User in Controller using Facade
3) Laravel 10 Get Current User in View Blade using Helper
4) Laravel 10 Get Current User in View Blade using Facade
So, let's see I added two ways to get current user data in the laravel 10 application.
1) Laravel 10 Get Current User in Controller using Helper
Here, we will get current user data using auth() in laravel 10.
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class UserController extends Controller
{
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index(Request $request)
{
/* Current Login User Details */
$user = auth()->user();
var_dump($user);
/* Current Login User ID */
$userID = auth()->user()->id;
var_dump($userID);
/* Current Login User Name */
$userName = auth()->user()->name;
var_dump($userName);
/* Current Login User Email */
$userEmail = auth()->user()->email;
var_dump($userEmail);
}
}
2) Laravel 10 Get Current User in Controller using Facade
Here, we will get current user data using Auth facade in laravel 10.
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Auth;
class UserController extends Controller
{
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index(Request $request)
{
/* Current Login User Details */
$user = Auth::user();
var_dump($user);
/* Current Login User ID */
$userID = Auth::user()->id;
var_dump($userID);
/* Current Login User Name */
$userName = Auth::user()->name;
var_dump($userName);
/* Current Login User Email */
$userEmail = Auth::user()->email;
var_dump($userEmail);
}
}
3) Laravel 10 Get Current User in View Blade using Helper
Here, we will get current user data using auth() helper in laravel 10.
<p> User ID: {{ auth()->user()->id }} </p>
<p> User Name: {{ auth()->user()->name }} </p>
<p> User Email: {{ auth()->user()->email }} </p>
4) Laravel 10 Get Current User in View Blade using Facade
Here, we will get current user data using Auth Facade in laravel 10.
<p> User ID: {{ Auth::user()->id }} </p>
<p> User Name: {{ Auth::user()->name }} </p>
<p> User Email: {{ Auth::user()->email }} </p>
- How to Notifications With database Driver in Laravel 11?
- How to Send Email Via Notification in Laravel 11?
- How to Install and Configure Laravel 11 Debugbar?
- How to Like Dislike System in Laravel 11?
- How to Paginate a Model with Relationship in Laravel 11?
- Laravel 11 Upload Files to Amazon S3 Example
- Laravel 11 Send WhatsApp Messages Example
- Laravel 11 Rest API Authentication Using JWT Tutorial