Home Arrow Icon Knowledge base Arrow Icon Global Arrow Icon How do I send parameters to a job in Laravel


How do I send parameters to a job in Laravel


To send parameters to a job in Laravel, you can use the `dispatch` method and pass the required arguments to the job's constructor. Here are the steps:

1. Define the Job Class:
- Create a job class that accepts parameters in its constructor. For example, `SiteCrawlerJob`:

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 SiteCrawlerJob implements ShouldQueue
     {
         use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;

         public function __construct(public Site $site, public Crawler $crawler)
         {
             // Constructor logic
         }

         public function handle(): void
         {
             // Job logic
         }
     }
     

2. Dispatch the Job:
- In your controller, dispatch the job with the required parameters:

php
     namespace App\Http\Controllers;

     use App\Http\Controllers\Controller;
     use App\Jobs\SiteCrawlerJob;
     use Illuminate\Http\Request;
     use Illuminate\Http\RedirectResponse;

     class PodcastController extends Controller
     {
         public function store(Request $request): RedirectResponse
         {
             $podcast = Podcast::create(/* ... */);
             // Dispatch the job
             SiteCrawlerJob::dispatch($podcast);
             return redirect('/podcasts');
         }
     }
     

3. Passing Data to Jobs:
- You can pass data to jobs using the `dispatch` method. For example, if you need to pass a `Site` model and a `Crawler` instance:

php
     public function store(Request $request): RedirectResponse
     {
         $podcast = Podcast::create(/* ... */);
         // Dispatch the job
         SiteCrawlerJob::dispatch(new SiteCrawlerJob($podcast));
         return redirect('/podcasts');
     }
     

4. Handling Multiple Parameters:
- If you need to pass multiple parameters to a job, you can create a Data Transfer Object (DTO) to encapsulate the data. For example, `SiteCrawlerTransporter`:

php
     namespace App\Transporters;

     use App\Models\Site;
     use Symfony\Component\DomCrawler\Crawler;

     class SiteCrawlerTransporter
     {
         public function __construct(public Site $site, public Crawler $crawler)
         {
             // Constructor logic
         }
     }
     

5. Using Pipelines:
- You can use Laravel's pipeline feature to process data through a series of tasks. For example, you can create a pipeline to crawl a site:

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\Pipeline;

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

         public function __construct(public Site $site)
         {
             // Constructor logic
         }

         public function handle(): void
         {
             // Pipeline logic
             Pipeline::send(new SiteCrawlerTransporter($site, new Crawler(Http::get($site->domain)->body())))
                 ->through([
                     function (SiteCrawlerTransporter $payload, Closure $next) {
                         // Process the payload
                         return $next($payload);
                     },
                 ])
                 ->thenReturn();
         }
     }
     

By following these steps, you can effectively pass parameters to jobs in Laravel and manage complex data processing tasks efficiently[1][2][3].

Citations:
[1] https://www.reddit.com/r/laravel/comments/7x0dya/passing_data_to_laravel_jobs/
[2] https://www.luckymedia.dev/blog/laravel-pipelines-sending-multiple-parameters
[3] https://laravel.com/docs/11.x/queues
[4] https://github.com/laravel/framework/issues/21339
[5] https://www.geeksforgeeks.org/different-ways-for-passing-data-to-view-in-laravel/