0
0
LaravelHow-ToBeginner · 4 min read

How to Create a Job in Laravel: Step-by-Step Guide

In Laravel, create a job using the php artisan make:job JobName command, then define the task in the job's handle() method. Dispatch the job with dispatch(new JobName()) to run it asynchronously or immediately.
📐

Syntax

Use the artisan command to create a job class, then implement the handle() method where the job's task is defined. Dispatch the job using the dispatch() helper.

  • php artisan make:job JobName: Creates the job class.
  • handle(): Method where job logic goes.
  • dispatch(new JobName()): Sends the job to the queue or runs immediately.
bash
php artisan make:job JobName

// In app/Jobs/JobName.php
public function handle()
{
    // Job logic here
}

// Dispatching the job
use App\Jobs\JobName;
dispatch(new JobName());
💻

Example

This example creates a job that logs a message. It shows how to generate the job, add logic, and dispatch it.

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;
use Illuminate\Support\Facades\Log;

class LogMessageJob implements ShouldQueue
{
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;

    protected string $message;

    public function __construct(string $message)
    {
        $this->message = $message;
    }

    public function handle(): void
    {
        Log::info('Job message: ' . $this->message);
    }
}

// Dispatch the job somewhere in your code
use App\Jobs\LogMessageJob;
dispatch(new LogMessageJob('Hello from Laravel Job!'));
Output
No visible output; the message 'Job message: Hello from Laravel Job!' is logged in storage/logs/laravel.log
⚠️

Common Pitfalls

Common mistakes include:

  • Not implementing ShouldQueue interface, so the job runs immediately instead of queued.
  • Forgetting to import the job class before dispatching.
  • Not configuring queue drivers, causing jobs not to run asynchronously.
  • Placing heavy logic outside the handle() method, which runs on job creation instead of execution.
php
<?php
// Wrong: Missing ShouldQueue interface means job runs immediately
class ExampleJob
{
    public function handle()
    {
        // logic
    }
}

// Right: Implements ShouldQueue for queued execution
use Illuminate\Contracts\Queue\ShouldQueue;

class ExampleJob implements ShouldQueue
{
    public function handle()
    {
        // logic
    }
}
📊

Quick Reference

StepCommand / CodeDescription
1php artisan make:job JobNameCreate a new job class
2Implement handle() methodDefine the job's task inside this method
3dispatch(new JobName())Send the job to the queue or run immediately
4Configure queue driverSet up queue in config/queue.php for async processing
5Run queue workerUse php artisan queue:work to process queued jobs

Key Takeaways

Create jobs with 'php artisan make:job JobName' and define logic in handle().
Implement ShouldQueue interface to run jobs asynchronously.
Dispatch jobs using dispatch(new JobName()) to queue or run immediately.
Configure queue drivers and run queue workers for background processing.
Avoid putting heavy logic in the constructor; keep it inside handle().