Creating and Dispatching Jobs
Generate a job class with Artisan and dispatch it from controllers.
Creating and Dispatching Jobs is a free PHP Academy lesson on CoddyKit — lesson 2 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the PHP Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
What is a Job?
A job is a PHP class that encapsulates a single unit of queued work. Each job implements handle(), which the queue worker calls to execute the task.
Generating a Job
Use Artisan to create a job class. The --queue flag makes it queuable (default).
$ php artisan make:job SendWelcomeEmailJob Class Structure
A job has a handle() method and can receive data via its constructor.
<?php
namespace App\Jobs;
use App\Models\User;
use App\Mail\WelcomeEmail;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\SerializesModels;
class SendWelcomeEmail implements ShouldQueue
{
use Dispatchable, Queueable, SerializesModels;
public function __construct(private User $user) {}
public function handle(): void
{
Mail::to($this->user)->send(new WelcomeEmail($this->user));
}
}Dispatching a Job
Dispatch the job from anywhere in your application.
<?php
// Dispatch to queue:
SendWelcomeEmail::dispatch($user);
// Dispatch with delay:
SendWelcomeEmail::dispatch($user)->delay(now()->addMinutes(10));
// Dispatch synchronously (bypass queue):
SendWelcomeEmail::dispatchSync($user);Dispatch Helpers
You can also use the dispatch() global helper.
<?php
dispatch(new SendWelcomeEmail($user))
->onQueue("emails")
->delay(now()->addHour());onQueue()
Target a specific named queue when dispatching.
<?php
SendWelcomeEmail::dispatch($user)->onQueue("emails");onConnection()
Target a specific queue connection (e.g. SQS for one job, Redis for another).
<?php
SendWelcomeEmail::dispatch($user)->onConnection("sqs");Serializing Models
The SerializesModels trait ensures Eloquent models passed to the job are serialized by ID and re-fetched from the database by the worker — avoiding stale data.
afterResponse()
Dispatch a job after the HTTP response is sent to the user — the user does not wait for the dispatch overhead.
<?php
App::terminating(fn () => SendWelcomeEmail::dispatch($user));Testing Jobs
Use Queue::fake() in tests to assert jobs are dispatched without actually running them.
<?php
Queue::fake();
$this->post("/register", $data);
Queue::assertPushed(SendWelcomeEmail::class);Unique Jobs
Implement ShouldBeUnique to prevent duplicate jobs for the same data from piling up in the queue.
Summary
Generate jobs with Artisan. Implement ShouldQueue and handle(). Dispatch with ::dispatch(). Target queues with onQueue(). Test with Queue::fake().
Quick Check
Which interface makes a job class queuable?
Frequently asked questions
Is the “Creating and Dispatching Jobs” lesson free?
Yes — the full text of “Creating and Dispatching Jobs” is free to read here on the web, and the PHP Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the PHP Academy course, upgrade to CoddyKit PRO.
What will I learn in “Creating and Dispatching Jobs”?
Generate a job class with Artisan and dispatch it from controllers. You practise PHP Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start PHP Academy?
No prior experience is required. PHP Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 2 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Creating and Dispatching Jobs” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this PHP Academy lesson?
Yes. Every PHP Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Queue Concepts and Drivers
- Creating and Dispatching Jobs
- Job Retries, Delays, and Chaining
- Monitoring Queues with Laravel Horizon