Home Arrow Icon Knowledge base Arrow Icon Global Arrow Icon How do I create a DTO in Laravel for job parameters


How do I create a DTO in Laravel for job parameters


To create a Data Transfer Object (DTO) in Laravel for job parameters, you can follow these 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. Define the DTO:
- Create a DTO class to encapsulate the data required by the job. 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
         }
     }
     

3. 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(new SiteCrawlerTransporter($podcast));
             return redirect('/podcasts');
         }
     }
     

4. Handling Multiple Parameters:
- If you need to pass multiple parameters to a job, you can create a 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 using DTOs and manage complex data processing tasks efficiently[1][2][3][4][5].

Citations:
[1] https://www.luckymedia.dev/blog/laravel-pipelines-sending-multiple-parameters
[2] https://www.twilio.com/en-us/blog/mastering-data-transfer-objects-laravel
[3] https://github.com/romanzipp/Laravel-DTO
[4] https://dev.to/zubairmohsin33/data-transfer-object-dto-in-laravel-with-php7-4-typed-properties-2hi9
[5] https://stackoverflow.com/questions/57978163/passing-parameters-to-laravel-job-is-not-working