ItSolutionStuff.com

Laravel 9 Get Current Logged in User Data Example

By Hardik Savani • November 5, 2023
Laravel

This post will give you an example of laravel 9 get current logged in user id. Here you will learn how to get the current user id in laravel 9. you'll learn laravel 9 get current user id. In this article, we will implement a get current user data laravel 9. You just need to some steps to done get the current user email in laravel 9.

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 9 Get Current User in Controller using Helper

2) Laravel 9 Get Current User in Controller using Facade

3) Laravel 9 Get Current User in View Blade using Helper

4) Laravel 9 Get Current User in View Blade using Facade

So, let's see I added two ways to get current user data in the laravel 9 application.

1) Laravel 9 Get Current User in Controller using Helper

Here, we will get current user data using auth() in laravel 9.

<?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 9 Get Current User in Controller using Facade

Here, we will get current user data using Auth facade in laravel 9.

<?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 9 Get Current User in View Blade using Helper

Here, we will get current user data using auth() helper in laravel 9.

<p> User ID: {{ auth()->user()->id }} </p>

<p> User Name: {{ auth()->user()->name }} </p>

<p> User Email: {{ auth()->user()->email }} </p>

4) Laravel 9 Get Current User in View Blade using Facade

Here, we will get current user data using Auth Facade in laravel 9.

<p> User ID: {{ Auth::user()->id }} </p>

<p> User Name: {{ Auth::user()->name }} </p>

<p> User Email: {{ Auth::user()->email }} </p>

I hope it can help you...

Hardik Savani

Hardik Savani

I'm a full-stack developer, entrepreneur, and founder of ItSolutionStuff.com. Passionate about PHP, Laravel, JavaScript, and helping developers grow.

📺 Subscribe on YouTube

We Are Recommending You

Laravel 9 Pagination Example Tutorial

Read Now →

Laravel 9 Multi Auth: Create Multiple Authentication in Laravel

Read Now →

Laravel 9 REST API with Passport Authentication Tutorial

Read Now →

Laravel 9 Scout Full Text Search Tutorial

Read Now →

Laravel 9 Drag and Drop File Upload with Dropzone JS

Read Now →

Laravel 9 Cron Job Task Scheduling Tutorial

Read Now →

Laravel 9 Livewire CRUD using Jetstream & Tailwind CSS

Read Now →

Laravel 9 Clear Cache of Route, View, Config, Event Commands

Read Now →

Laravel 9 Database Seeder Tutorial Example

Read Now →

Laravel 9 REST API Authentication using Sanctum Tutorial

Read Now →

Laravel 9 Auth with Inertia JS Jetstream Example

Read Now →