In this tutorial, I will discuss how to get current logged in user information in controller and blade in laravel 9. We can get all the information of the current logged in user using auth() helper or Auth facades. We can use one of the to get current logged in user data in Laravel.
See the example code of how to get current logged in user information in laravel controller:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
class TestController extends Controller
{
public function index()
{
//logged in users using Auth facades
$user = Auth::user();
//logged in users using auth() helper
$user = auth()->user();
}
}
See the example code of how to get current logged in user information in laravel blade:
<!DOCTYPE html>
<html lang="{{ str_replace('_', '-', app()->getLocale()) }}">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Laravel</title>
</head>
<body>
@auth
{{ auth()->user() }}
@endauth
</body>
</html>
We can directly get the current logged in user id like:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
class TestController extends Controller
{
public function index()
{
//logged in user ID using Auth facades
$user = Auth::id();
//logged in user ID using auth() helper
$user = auth()->id();
}
}
Read also: How To Send Mail In Laravel 9 Using Queue
Hope it can help you.