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.
- 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
- PHP 8.1+
- Composer
- Redis
- Node.js / npm (for Laravel Mix if needed)
- Clone the repository:
git clone <repo-url>
cd csv-upload- Install dependencies:
composer install- Copy
.envfile:
cp .env.example .env- 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- Create SQLite database file:
touch database/database.sqlite- Run migrations:
php artisan migrate- Install Laravel Horizon:
php artisan horizon:install
php artisan migrateStart the application:
php artisan serveStart Redis queue worker:
php artisan queue:work redisStart Laravel Horizon dashboard:
php artisan horizonStart WebSocket server:
php artisan websockets:serveVisit:
- App: http://localhost:8000
- Horizon Dashboard: http://localhost:8000/horizon
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_KEYwill be updated, not duplicated
- Upload CSV using the form on
/ - Recent uploads are listed with status:
pending,processing,completed,failed - Status updates in real-time via WebSocket
- 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' => ...], [...])
- Laravel WebSockets used for broadcasting upload status updates
- Frontend listens on
uploadschannel - Event:
App\Events\UploadStatusUpdated