How to Dispatch Job in Laravel: Simple Guide with Examples
In Laravel, you dispatch a job by calling
dispatch(new JobClass) or using the JobClass::dispatch() method. This sends the job to the queue system to run asynchronously or immediately depending on your configuration.Syntax
To dispatch a job in Laravel, you typically use the dispatch() helper function or the static dispatch() method on the job class.
dispatch(new JobClass): Creates a new job instance and sends it to the queue.JobClass::dispatch(): A shortcut to dispatch the job without manually creating an instance.
Jobs are PHP classes that implement the ShouldQueue interface to run asynchronously.
php
dispatch(new App\Jobs\ProcessPodcast($podcast)); // Or using static dispatch method App\Jobs\ProcessPodcast::dispatch($podcast);
Example
This example shows how to create a job class and dispatch it with data. The job will process the data asynchronously.
php
<?php namespace App\Jobs; use Illuminate\Bus\Queueable; use Illuminate\Contracts\Queue\ShouldQueue; use Illuminate\Foundation\Bus\Dispatchable; use Illuminate\Queue\InteractsWithQueue; use Illuminate\Queue\SerializesModels; class SendWelcomeEmail implements ShouldQueue { use Dispatchable, InteractsWithQueue, Queueable, SerializesModels; protected $userEmail; public function __construct(string $userEmail) { $this->userEmail = $userEmail; } public function handle() { // Logic to send email // For example: Mail::to($this->userEmail)->send(new WelcomeEmail()); echo "Welcome email sent to {$this->userEmail}\n"; } } // Dispatching the job SendWelcomeEmail::dispatch('user@example.com');
Output
Welcome email sent to user@example.com
Common Pitfalls
Common mistakes when dispatching jobs include:
- Not implementing
ShouldQueueinterface, causing the job to run immediately instead of queued. - Forgetting to import the job class or using wrong namespaces.
- Not configuring the queue driver, so jobs do not run asynchronously.
- Dispatching jobs without required constructor parameters.
Always check your queue configuration and run php artisan queue:work to process queued jobs.
php
<?php // Wrong: Job class missing ShouldQueue interface class ProcessData { public function handle() { // ... } } // Right: Implements ShouldQueue use Illuminate\Contracts\Queue\ShouldQueue; class ProcessData implements ShouldQueue { public function handle() { // ... } }
Quick Reference
| Action | Code Example | Description |
|---|---|---|
| Dispatch job instance | dispatch(new JobClass($data)); | Send a new job instance to the queue |
| Dispatch using static method | JobClass::dispatch($data); | Shortcut to dispatch job without new keyword |
| Implement queue interface | class JobClass implements ShouldQueue | Make job run asynchronously |
| Run queue worker | php artisan queue:work | Process queued jobs in background |
Key Takeaways
Use dispatch(new JobClass) or JobClass::dispatch() to send jobs to the queue.
Ensure your job class implements ShouldQueue to run asynchronously.
Configure your queue driver and run queue workers to process jobs.
Pass required data via the job constructor when dispatching.
Check namespaces and imports to avoid class not found errors.