Queues
A job class
A job is a unit of background work such as sending a report or resizing images. The class implements the ShouldQueue contract and puts the work inside a handle method.
The web request only creates the job object, so the visitor never waits for the slow part.
use Illuminate\Contracts\Queue\ShouldQueue; class SendReport implements ShouldQueue { public function handle(): void { echo "Report sent"; } }
Dispatch
Dispatch the job from a controller or a command with the static dispatch method. Laravel serializes the job and stores it in the configured queue backend.
Arguments passed to dispatch reach the constructor, so the job carries everything handle needs.
SendReport::dispatch(); SendReport::dispatch(42);
Run the worker
Queued jobs wait until a worker process picks them up. Start one with the canonical work command on the machine that should process the queue.
php artisan queue:work
INFO Processing jobs from the queue.
Keep the worker running with a process monitor on live servers so it restarts after reboots and failures.
Database driver note
The simplest backend for learning is the database driver: jobs wait as rows in a table and need no extra service. Read the active connection name with the config helper when debugging.
Switch to a dedicated service such as Redis only when the project outgrows plain rows.
$driver = config("queue.default"); echo $driver;
Next: Mail
Article author: Arthur Isaev