Laravel 11 Multiple Image Upload Tutorial with Example

Laravel 11 multiple image upload validation; Through this tutorial, i am going to show you how to upload multiple image in Laravel 11 with validation.

In this post example, i will build multiple image upload form in Laravel 11 with bootstrap library. As well as i will validate multiple image mime type, size before upload/save to database and directory in Laravel 11 apps.

Laravel 11 Multiple Image Upload Tutorial with Example

Use the below given simple steps to upload multiple images in Laravel 11 app:

  • Step 1 – Install Laravel 11 Application
  • Step 2 – Database Configuration
  • Step 3 – Create Model & Migration
  • Step 4 – Create Multiple Image Upload Routes
  • Step 5 – Creating Controller
  • Step 6 – Create Blade View
    • Create Multiple image upload form
  • Step 7 – Start Development Server
  • Step 8 – Run Laravel 11 Multiple Image Upload with Validation App On Browser

Step 1 – Install Laravel 11 Application

In step 1, open your terminal and navigate to your local web server directory using the following command:

//for windows user
cd xampp/htdocs

//for ubuntu user
cd var/www/html

Then install laravel latest application using the following command:

composer create-project --prefer-dist laravel/laravel LaravelMultipleImageUpload

Step 2 – Database Configuration

In step 2, open your downloaded Laravel 11 app into any text editor. Then find .env file and configure database detail like following:

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=db name
DB_USERNAME=db user name
DB_PASSWORD=db password

Step 3 – Create Model & Migration

In step 3, open command prompt and navigate to your project by using the following command:

cd / LaravelMultipleImageUpload

Then create model and migration file by using the following command:

php artisan make:model Image -m

The above command will create two files into your laravel multiple image upload with validation tutorial app, which is located inside the following locations:

  • /app/Models/Image.php
  • /database/migrations/create_images_table.php

So, find create_images_table.php file inside /database/migrations/ directory. Then open this file and add the following code into function up() on this file:

    public function up()
    {
        Schema::create('images', function (Blueprint $table) {
            $table->id();
            $table->string('name');
            $table->string('path');
            $table->timestamps();
        });
    }

Now, open again your terminal and type the following command on cmd to create tables into your selected database:

php artisan migrate

Step 4 – Create Multiple Image Upload Routes

In step 4, open your web.php file, which is located inside routes directory. Then add the following routes into web.php file:

use App\Http\Controllers\UploadMultipleImageController;

Route::get('upload-multiple-image', [UploadMultipleImageController::class, 'index']);

Route::post('store-images', [UploadMultipleImageController::class, 'store']);

Step 5 – Creating Controller

In step 5, create image upload controller by using the following command:

php artisan make:controller UploadMultipleImageController

The above command will create UploadMultipleImageController.php file, which is located inside /app/Http/Controllers/ directory.

The following laravel validation rules will validate multiple image file before upload/save into database and storage directory:

     $validatedData = $request->validate([
	    'images' => 'required',
            'images.*' => 'mimes:jpg,png,jpeg,gif,svg'
        ]);

So open UploadMultipleImageController.php file and add the following code into it:

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\Models\Image;

class UploadMultipleImageController extends Controller
{
   public function index()
    {
        return view('upload-multiple-image');
    }

    public function store(Request $request)
    {
        
        $validatedData = $request->validate([
	    'images' => 'required',
        'images.*' => 'mimes:jpg,png,jpeg,gif,svg'
        ]);


        if($request->hasfile('images'))
         {
            foreach($request->file('images') as $key => $file)
            {
                $path = $file->store('public/images');
                $name = $file->getClientOriginalName();

                $insert[$key]['name'] = $name;
                $insert[$key]['path'] = $path;

            }
         }

        Image::insert($insert);

        return redirect('upload-multiple-image')->with('status', 'Multiple Image has been uploaded into db and storage directory');

    }
}

The following code will upload multiple image inside storage/app/public/images directory:

  $path = $file->store('public/images');        

Step 6 – Create Blade Views

In step 6, create new blade view file that named multiple-image-upload.blade.php inside resources/views directory for upload multiple image form.

  • Create Multiple image upload form

I have already created multiple image upload form in laravel, so you can add the following code into multiple-image-upload.blade.php:

<!DOCTYPE html>
<html>
<head>
  <title>How to Upload Multiple Images In Laravel 11 With Validation</title>

  <meta name="csrf-token" content="{{ csrf_token() }}">

  <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css">

</head>
<body>

<div class="container mt-5">

  @if(session('status'))
    <div class="alert alert-success">
        {{ session('status') }}
    </div>
  @endif

  <div class="card">

    <div class="card-header text-center font-weight-bold">
      <h2>Laravel 11 Upload Multiple Image With Validation</h2>
    </div>

    <div class="card-body">
        <form name="upload-multiple-image" method="POST"  action="{{ url('store-images') }}" accept-charset="utf-8" enctype="multipart/form-data">

          @csrf
                  
            <div class="row">

                <div class="col-md-12">
                    <div class="form-group">
                        <input type="file" name="images[]" placeholder="Choose images" multiple >
                    </div>
                    @error('images')
                        <div class="alert alert-danger mt-1 mb-1">{{ $message }}</div>
                    @enderror
                </div>
                  
                <div class="col-md-12">
                    <button type="submit" class="btn btn-primary" id="submit">Submit</button>
                </div>
            </div>     
        </form>

    </div>

  </div>

</div>  
</body>
</html>

The following code display error message on laravel multiple image upload forms. So do not forget to add the following code along laravel forms fields:

          @error('images')
              <div class="alert alert-danger mt-1 mb-1">{{ $message }}</div>
          @enderror

Don’t worry i have already added the validation error message display code along with each form fields.

Step 7 – Start Development Server

Finally, open your command prompt again and run the following command to start development server for your Laravel 11 image upload application:

php artisan serve

Step 8 – Run Laravel 11 Multiple Image Upload with Validation App On Browser

In step 8, open your browser and fire the following url into your browser:

http://127.0.0.1:8000/upload-multiple-image

Note that, in this example, the multiple images will be upload on the following path – storage/app/public/images.

Recommended Laravel Tutorials

Leave a Comment