Skip to content

A Laravel 9 project to upload CSV files in the background, process data into SQLite, and show real-time upload status using WebSockets. Supports upsert by UNIQUE_KEY and avoids duplicate entries.

Notifications You must be signed in to change notification settings

haryokuncoro/csv-upload

Repository files navigation

Laravel CSV Upload (Laravel 9 + PHP 8.1)

A Laravel 9 project to upload CSV files in the background, process data into SQLite, and show real-time upload status using WebSockets. Supports upsert by UNIQUE_KEY and avoids duplicate entries.


Features

  • Laravel 9 + PHP 8.1
  • SQLite database
  • Redis for queue management
  • Laravel Horizon for queue monitoring
  • Background processing of CSV uploads
  • Real-time upload status via WebSocket
  • CSV data cleaned to UTF-8
  • Upsert functionality based on UNIQUE_KEY
  • Avoid duplicate entries on multiple uploads
  • Frontend UI to upload CSV and view recent uploads

Requirements

  • PHP 8.1+
  • Composer
  • Redis
  • Node.js / npm (for Laravel Mix if needed)

Installation

  1. Clone the repository:
git clone <repo-url>
cd csv-upload
  1. Install dependencies:
composer install
  1. Copy .env file:
cp .env.example .env
  1. Set environment variables in .env:
APP_NAME=LaravelCSV
APP_URL=http://localhost

DB_CONNECTION=sqlite

QUEUE_CONNECTION=redis
REDIS_HOST=127.0.0.1
REDIS_PASSWORD=null
REDIS_PORT=6379

BROADCAST_DRIVER=pusher
PUSHER_APP_ID=local
PUSHER_APP_KEY=local
PUSHER_APP_SECRET=local
PUSHER_HOST=127.0.0.1
PUSHER_PORT=6001
PUSHER_SCHEME=http
PUSHER_APP_CLUSTER=mt1
  1. Create SQLite database file:
touch database/database.sqlite
  1. Run migrations:
php artisan migrate
  1. Install Laravel Horizon:
php artisan horizon:install
php artisan migrate

Running the Project

Start the application:

php artisan serve

Start Redis queue worker:

php artisan queue:work redis

Start Laravel Horizon dashboard:

php artisan horizon

Start WebSocket server:

php artisan websockets:serve

Visit:


CSV Format

The CSV should have the following fields:

UNIQUE_KEY,PRODUCT_TITLE,PRODUCT_DESCRIPTION,STYLE#,SANMAR_MAINFRAME_COLOR,SIZE,COLOR_NAME,PIECE_PRICE
  • Non-UTF8 characters will be cleaned automatically
  • Rows with existing UNIQUE_KEY will be updated, not duplicated

Frontend UI

  • Upload CSV using the form on /
  • Recent uploads are listed with status: pending, processing, completed, failed
  • Status updates in real-time via WebSocket

Jobs & Background Processing

  • CSV processing is handled in app/Jobs/ProcessCsvUpload.php
  • Each upload dispatches a queued job
  • Horizon monitors queue jobs in real-time
  • Upsert handled using Product::updateOrCreate(['unique_key' => ...], [...])

WebSockets

  • Laravel WebSockets used for broadcasting upload status updates
  • Frontend listens on uploads channel
  • Event: App\Events\UploadStatusUpdated

About

A Laravel 9 project to upload CSV files in the background, process data into SQLite, and show real-time upload status using WebSockets. Supports upsert by UNIQUE_KEY and avoids duplicate entries.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published