Why can’t `$request->user()` be accessed in global Laravel middleware even when the proper prerequisite middleware is called?

This is more of an academic question than a real-world one, although it stumped me when I came across it in a legacy 5.2 project with misconfigured middleware, and its behavior hasn’t changed in Laravel 11. While similar questions have been asked across the internet, the outcome has always been someone forgetting to move the appropriate middleware before their failing middleware. This is different.

Some context: out of the box, Laravel comes with global, “web”, and “api” middleware groups. Once a user has been authenticated via a regular cookie-based session, the IlluminateCookieMiddlewareEncryptCookies::class and IlluminateSessionMiddlewareStartSession::class middlewares are the only ones required to load up that user to be accessed via auth()->user() or $request->user(). These middlewares are included as part of the default “web” group.

Middleware is executed sequentially, in the order it is listed. Global middleware is always executed first. However, promoting the aforementioned “web” middleware to global still works, but causes $request->user() to become null when referenced from global middleware called just after. In other words:

// app/Http/Middleware/Test.php

<?php

namespace AppHttpMiddleware;

use Closure;
use IlluminateHttpRequest;

class Test
{
    public function handle(Request $request, Closure $next)
    {
        dd($request->user()); // `null` when global and `AppModelsUser` when not
        dd(auth()->user()); // This always works
    }
}

Here’s the rest of the code:

// bootstrap/app.php

<?php

use IlluminateFoundationApplication;
use IlluminateFoundationConfigurationExceptions;
use IlluminateFoundationConfigurationMiddleware;

return Application::configure(basePath: dirname(__DIR__))
    ->withRouting(
        web: __DIR__.'/../routes/web.php',
    )
    ->withMiddleware(function (Middleware $middleware) {

        // This doesn't work...

        $middleware->use([
            IlluminateCookieMiddlewareEncryptCookies::class,
            IlluminateSessionMiddlewareStartSession::class,
            AppHttpMiddlewareTest::class,
        ]);

        $middleware->group('web', []); // Clear out default web middleware


        // But this works...

        $middleware->use([
            IlluminateCookieMiddlewareEncryptCookies::class,
            IlluminateSessionMiddlewareStartSession::class,
        ]);

        $middleware->group('web', [
            AppHttpMiddlewareTest::class,
        ]);


        // And this also works...

        $middleware->use([]); // Clear out default global middleware

        $middleware->group('web', [
            IlluminateCookieMiddlewareEncryptCookies::class,
            IlluminateSessionMiddlewareStartSession::class,
            AppHttpMiddlewareTest::class,
        ]);

    })
    ->create();
// routes/web.php

<?php

use IlluminateSupportFacadesRoute;

Route::get('', fn() => 'It works!');

I did a little digging into Laravel source, but quickly got in over my head. Notably, $request->user() is always set by the time of this line of StartSession, regardless if it’s global or not.

I’m satisfied that this behavior is consistent and seemingly intended, but I’m hoping someone more knowledgeable can explain why this behaves this way.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật