Home Arrow Icon Knowledge base Arrow Icon Global Arrow Icon How do I create a custom job in Laravel


How do I create a custom job in Laravel


To create a custom job in Laravel, follow these steps:

1. Create a New Job Class:
- Run the following command in your terminal to generate a new job class:

bash
     php artisan make:job CustomJob
     

- This will create a new PHP class in the `app/Jobs` directory.

2. Define the Job's Logic:
- Open the newly created `CustomJob.php` file and define the logic for the job within the `handle()` method. This method contains the code that will be executed when the job is dispatched:

php
     namespace App\Jobs;

     use Illuminate\Bus\Queueable;
     use Illuminate\Queue\SerializesModels;
     use Illuminate\Queue\InteractsWithQueue;
     use Illuminate\Contracts\Queue\ShouldQueue;
     use Illuminate\Foundation\Bus\Dispatchable;

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

         /**
          * Execute the job.
          *
          * @return void
          */
         public function handle()
         {
             // Your custom job logic goes here
         }
     }
     

3. Dispatch the Job:
- To dispatch the job, you can use the `dispatch()` method and pass an instance of the job class as an argument:

php
     CustomJob::dispatch();
     

4. Process the Queue:
- To process the queue, you need to start the queue worker using the following command:

bash
     php artisan queue:work
     

The queue worker will execute each job by calling its `handle()` method and then mark the job as completed. If an error occurs while processing a job, the worker will retry the job a certain number of times before moving it to the failed job queue[1][3][5].

Here is a detailed example of creating a custom job in Laravel:

Example: Sending an Email Job

1. Create the Job Class:
- Run the following command to generate a new job class:

bash
     php artisan make:job SendEmailJob
     

2. Define the Job's Logic:
- Open the `SendEmailJob.php` file and define the logic for the job within the `handle()` method:

php
     namespace App\Jobs;

     use Illuminate\Bus\Queueable;
     use Illuminate\Queue\SerializesModels;
     use Illuminate\Queue\InteractsWithQueue;
     use Illuminate\Contracts\Queue\ShouldQueue;
     use Illuminate\Foundation\Bus\Dispatchable;

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

         /**
          * Execute the job.
          *
          * @return void
          */
         public function handle()
         {
             // Send email logic here
         }
     }
     

3. Dispatch the Job:
- To dispatch the job, you can use the `dispatch()` method and pass an instance of the job class as an argument:

php
     SendEmailJob::dispatch();
     

4. Process the Queue:
- To process the queue, you need to start the queue worker using the following command:

bash
     php artisan queue:work
     

The queue worker will execute each job by calling its `handle()` method and then mark the job as completed. If an error occurs while processing a job, the worker will retry the job a certain number of times before moving it to the failed job queue[1][3][5].

Conclusion

Creating custom jobs in Laravel allows you to execute tasks asynchronously, enhancing application performance and scalability. By following these steps, you can create and manage custom jobs to automate various tasks in your application.

Citations:
[1] https://www.youtube.com/watch?v=BaZbuDL6nV8
[2] https://joedixon.co.uk/creating-a-custom-queue-driver-for-laravel
[3] https://dev.to/codeofaccuracy/laravel-jobqueue-2f1p
[4] https://stillat.com/blog/2016/12/07/laravel-artisan-generator-command-the-makejob-command
[5] https://www.twilio.com/en-us/blog/queueing-in-laravel