Fermin Perdomo

Senior Full Stack Engineer | PHP | JavaScript

Handling CSV file uploads is a common feature in many web applications, especially dashboards and admin panels. However, processing large CSV files during the request lifecycle can slow down your app. Instead, we can offload the heavy lifting to a background job using Laravel Queues.

In this article, you'll learn how to:
  • Upload and store a CSV file

  • Dispatch a job to process the file asynchronously

  • Save the data into a database

  • 🛠 Prerequisites
  • Laravel 10+

  • Queue configured (e.g. database, redis, sqs)

  • A simple database table to insert records

  • 1️⃣ Set Up the DatabaseLet’s assume you’re importing users. Create a migration:Run it:2️⃣ File Upload FormCreate a simple blade file:<!-- resources/views/upload.blade.php --> <form action="{{ route('csv.upload') }}" method="POST" enctype="multipart/form-data"> @csrf <input type="file" name="csv" accept=".csv" required> <button type="submit">Upload</button> </form>3️⃣ Routes and ControllerCreate a controller to handle upload:Define the routes:4️⃣ Create the Job to Process CSV:5️⃣ Set Up QueueIf using database driver, configure .env:Create the queue tables:Then run the queue worker:✅ Example CSV FormatMake sure your CSV is in this format:You can follow the next steps to improve it by:
  • Adding try/catch in the job

  • Logging errors

  • Using Laravel’s failed() method on the job class

  • Adding progress tracking in the database

  • 🔁 Step 6: Add Progress TrackingCreate a new table to track each CSV upload's progress:Run the migration:Update the upload method to create a tracking record:🔄 Step 7: Update the Job for Safe ExecutionUpdate ProcessCsv to handle errors and update progress.🚀 ConclusionProcessing CSV files in the background using Laravel Queues ensures your application stays responsive and scalable. This technique is perfect for bulk imports and large data operations.