How to Get Logged in User Data in Laravel 8 Tutorial

Share this Article
Reading Time: 3 minutes
622 Views

In laravel if you are developing any application in which authentication is working, then sure you should know how to get user data. This article is going to give an overview of methods by the help of which you can get logged in user details in laravel 8.

Inside this article we will see the available methods to get logged in user data in laravel 8.

We have two available options to get data –

  • Using Auth helper
  • Using Auth Facade

Let’s get started.


Installation of Laravel Application

Laravel Installation can be done in two ways.

  • Laravel Installer
  • By using composer

Laravel Installer

To install Laravel via Laravel installer, we need to install it’s installer first. We need to make use of composer for that.

$ composer global require laravel/installer

This command will install laravel installer at system. This installation is at global scope, so you type command from any directory at terminal. To verify type the given command –

$ laravel

This command will open a command palette of Laravel Installer.

To create ad install laravel project in system,

$ laravel new blog

With the name of blog a laravel project will be created at your specified path.

By using composer

Alternatively, we can also install Laravel by Composer command create-project. If your system doesn’t has Composer Installed, Learn Composer Installation Steps. Here is the complete command to create a laravel project-

$ composer create-project --prefer-dist laravel/laravel blog

After following these steps we can install a Laravel application into system.

To start the development server of Laravel –

$ php artisan serve

This command outputs –

Starting Laravel development server: http://127.0.0.1:8000

Assuming laravel already installed at system.


Working with Laravel Authentication

Here you can find some article links which will guide you about the integration of authentication in laravel 8.

By following these articles, you can create authentication system.


Get Logged In User Data

There are two options available to get logged in user data. these are followings –

  • Auth helper
  • Auth Facade

Auth Helper

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

You will get current user data using Auth Facade. Auth Facade you will find at 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 How to Get Logged in User Data in Laravel 8 Tutorial in a very detailed way.

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.

Find More on Laravel 8 Articles here

Leave a Comment