Start a Project

How to use Queue and Job in Laravel 10 ?

Today we’re going to learn about Laravel Job and Queue

When we builing the web application sometimes we need to read and write large amount of data but as we know php maximum time execution is 30 seconds if we upload or read big file data php will throw maximum time exception
Laravel provides a mechanism which allows us to read and write big file data data such as million and trillion of file data can easily read and write on the database
in background once it will finish it’s job Laravel will notified user that your job has been finished.

In this example, we are going to upload  csv file which has millions of records of an organization in our Bagisto App. Just follow the below step and make it done this example:

Firstly you need to create laravel module for bagisto in our case we’re created BulkUpload Module and enable it.

Step 1: Configure your .env file

Step 2: Generate migration file

Step 3: Create Routes for uploading.

Before creating the routes need to create Model, Controller and Repository associated with the Model.

Step 4: Create blade file for upload.

In our case we’re created the index.blade.php file for upload csv.

In BulkUploadController.php file in index method return the view of index.blade.php.

Step 5: Create a Job Class

In our case we have created UploadOrganizationCsv.php file for performing the specific task as Uploading the csv.

Step 5: In BulkUploadController store method.

In this store method we are validating the csv file. Reads entire file into an array.

Chunking the csv array into 1000 small parts .

Create a $header array variable for storing the csv header for create insertable array of each data we are chunking.

Now we have to dispatch the job and looping the chunked data and add job to the Bus  and  return the $batch. Now lets write code for UploadOrganizationCsv.php file for upload  as we can see we’re passed the two argument into the job $data and $header, need to accept these passed arguments into UploadOrganizationCsv.php

Step 6: In UploadOrganizationCsv.php

In Job handle method are implemented the logic of uploading the csv into the database.

 

That`s all for implementing the Laravel Job and Queue in Bagisto. Let us know your thoughts in the comments below.

You can also Hire Laravel Developers for build something amazing with Laravel and you also explore our  Bagisto Extensions.

Thanks for reading.

Exit mobile version