Navigation

Laravel

Laravel How to Get the Currently Authenticated User

Access the authenticated user in Laravel using Auth facade, auth() helper, or request object. Works in controllers, views, and middleware.

Problem: You need to access the currently logged-in user's information but aren't sure which method to use or where it's available.

Solution:

// In controllers - multiple ways
use Illuminate\Support\Facades\Auth;

public function profile()
{
    // Using Auth facade
    $user = Auth::user();
    $userId = Auth::id();
    
    // Using auth() helper
    $user = auth()->user();
    $userId = auth()->id();
    
    // Using request
    $user = request()->user();
    
    // Check if user is logged in
    if (Auth::check()) {
        $email = Auth::user()->email;
    }
}

// In Blade views
@auth
    <p>Welcome, {{ auth()->user()->name }}</p>
    <p>Email: {{ Auth::user()->email }}</p>
@endauth

@guest
    <p>Please log in</p>
@endguest

// In routes
Route::get('/dashboard', function () {
    return 'Hello ' . auth()->user()->name;
})->middleware('auth');

// Type-hinted in controller methods
public function update(Request $request)
{
    /** @var \App\Models\User $user */
    $user = $request->user();
    // Now you have IDE autocompletion
}

Why it works: Laravel automatically loads the authenticated user from the session. Auth::user() returns the full User model or null if not authenticated. Auth::id() returns just the ID without loading the full model. The auth() helper is a shortcut to the Auth facade.

Note: These methods return null if no user is authenticated, so always check or use auth middleware.

Table Of Contents

Related Topics

Share this article

Add Comment

No comments yet. Be the first to comment!

More from Laravel