Laravel 12 Multiple Image Upload: Step by Step Guide

In this tutorial, I’ll show you how to upload multiple images in Laravel 12. We’ll create a form, validate files, and store them in the storage folder. This feature is useful for galleries, profiles, and more.

Follow along to implement multiple image uploads easily in your Laravel project.

Laravel 12 Multiple Image Upload: Step by Step Guide

Laravel 12 Multiple Image Upload: Step by Step Guide

 

Step 1: Install Laravel 12

If you haven’t installed Laravel 12 yet, create a new project using:

composer create-project laravel/laravel laravel12-multi-image-upload

 

Step 2: Configure Database

Set up your database in the env file:

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=your_database_name
DB_USERNAME=your_username
DB_PASSWORD=your_password

Run migrations to set up default tables:

php artisan migrate

 

Step 3: Create Routes

Open routes/web.php and add these routes:

use App\Http\Controllers\ImageUploadController;
use Illuminate\Support\Facades\Route;

Route::get('upload-form', [ImageUploadController::class, 'showForm']);
Route::post('upload-images', [ImageUploadController::class, 'uploadImages']);

 

Step 4: Create Controller

Run the following command to create a controller:

php artisan make:controller ImageUploadController

Now open app/Http/Controllers/ImageUploadController.php and add this code:

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class ImageUploadController extends Controller
{
    public function showForm()
    {
        return view('upload-form');
    }

    public function uploadImages(Request $request)
    {
        $request->validate([
            'images.*' => 'required|image|mimes:jpeg,png,jpg,gif|max:2048',
        ]);

        foreach ($request->file('images') as $image) {
            $image->store('uploads', 'public');
        }

        return back()->with('success', 'Images uploaded successfully!');
    }
}

 

Step 5: Create View File

Create a new Blade file resources/views/upload-form.blade.php and add this code:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Laravel Multiple Image Upload</title>
</head>
<body>
    @if(session('success'))
        <p>{{ session('success') }}</p>
    @endif
    <form action="{{ url('upload-images') }}" method="POST" enctype="multipart/form-data">
        @csrf
        <input type="file" name="images[]" multiple>
        <button type="submit">Upload</button>
    </form>
</body>
</html>

 

Step 6: Create Storage Link

Run the following command to create a storage link:

php artisan storage:link

 

Step 7: Run Laravel Server

Now, start your Laravel development server:

php artisan serve

 


You might also like:

techsolutionstuff

Techsolutionstuff | The Complete Guide

I'm a software engineer and the founder of techsolutionstuff.com. Hailing from India, I craft articles, tutorials, tricks, and tips to aid developers. Explore Laravel, PHP, MySQL, jQuery, Bootstrap, Node.js, Vue.js, and AngularJS in our tech stack.

RECOMMENDED POSTS

FEATURE POSTS