Hello Dev,
In this tutorial, I will show you step-by-step how to upload files in Laravel 9 and automatically generate a UUID for each uploaded file. This is very useful when you want to uniquely identify uploaded files instead of only using their original names.
We’ll build a simple File Upload form in Laravel 9 where every file gets stored with a UUID in the database.
Step 1: Create a Fresh Laravel Project
First, create a new Laravel 9 project or use an existing one.
Step 2: Create Migration for File Uploads
Run the following command to create a migration file:
Now, open the migration file and update it as follows:
Run migration:
Step 3: Create Model
Generate a model for file uploads:
Step 4: Create Controller
Generate a controller:
Update app/Http/Controllers/FormController.php
:
Step 5: Define Routes
Open routes/web.php
:
Step 6: Create Blade View
Create a file at:
resources/views/form/form-upload-file.blade.php
Step 7: Run the Application
Start your Laravel server:
Visit:
👉 http://127.0.0.1:8000/form/upload
Upload a file, and each record will be saved in the database with a unique UUID. 🎉