Twinkle

defer() helper

Unlike queues, the defer() function executes some processing immediately after returning the response in the current request cycle.

https://laravel.com/docs/11.x/helpers#deferred-functions

With this, the logs will be recorded in the order of 1, 2, and 3.

// routes/web.php

use Illuminate\Support\Facades\Route;

Route::get('/', function () {
    info('1');

    defer(fn () => info('3'));

    info('2');

    return view('welcome');
});

As of Laravel 11.23, there is a bug in environments where Swoole is installed because Swoole also defines a global defer() function.
https://github.com/laravel/framework/issues/52774

PHP Fatal error: Uncaught Swoole\Error: API must be called in the coroutine

In the case of Windows + WSL (Ubuntu) + PHP 8.3, the issue can be resolved by removing Swoole.

sudo apt remove php8.3-swoole