Laravel 10 How To Get Logged In User Data Tutorial

Reading Time: 3 minutes
460 Views

When developing web apps, it is critical to get access to user data in order to provide personalised experiences and implement certain functionality. Laravel 10’s sophisticated authentication system makes it easier to retrieve logged-in user data. In this tutorial, we will walk you through the stages of retrieving and using logged-in user data in Laravel 10.

Laravel provides handy methods for accessing a user’s information once they have been authorised. We will go into these methods, investigate their applications, and illustrate how to integrate user data into the various components of your programme.

Read More: Laravel 10 Override Auth Register Method Tutorial

You have two available options to get data,

  • Using Auth helper
  • Using Auth Facade

Let’s get started.

Laravel Installation

Open terminal and run this command to create a laravel project.

composer create-project laravel/laravel myblog

It will create a project folder with name myblog inside your local system.

To start the development server of laravel –

php artisan serve

URL: http://127.0.0.1:8000

Assuming laravel already installed inside your system.

How To Add Laravel Authentication

There are following ways by which you can easily add an authentication system to your laravel application.

Once you have auth system then you case use the concept to get logged-in user data.

Auth Helper: Get Logged-in User Data

You will get current user data using auth().

$user = auth()->user();

By above code, you will get complete user information.

If you want to get user id then,

For User ID

echo $user->id

For name

echo $user->name

For email

echo $user->email

Auth Facade: Get Logged-in User Data

You will get current user data using Auth Facade. Auth Facade you will find in this location.

use Illuminate\Support\Facades\Auth;

Usage

$user = Auth::user();

By above code, you will get complete user information.

If you want to get user id then,

For User ID

echo $user->id

For name

echo $user->name

For email

echo $user->email

We hope this article helped you to learn about Laravel 10 How To Get Logged In User Data Tutorial in a very detailed way.

Online Web Tutor invites you to try Skillshike! Learn CakePHP, Laravel, CodeIgniter, Node Js, MySQL, Authentication, RESTful Web Services, etc into a depth level. Master the Coding Skills to Become an Expert in PHP Web Development. So, Search your favourite course and enroll now.

If you liked this article, then please subscribe to our YouTube Channel for PHP & it’s framework, WordPress, Node Js video tutorials. You can also find us on Twitter and Facebook.