How to Multiple Image Upload in Laravel 9?

Feb 11, 2022 . Admin


Hi friends,

This post will give you an example of how to multiple image upload in laravel 9?. I explained simply uploading an multiple images with validation in laravel 9. In this tutorial, you will learn how to multiple upload image into the database and storage directory with validation laravel 9. this example will help you upload image and display the uploaded multiple images laravel 9.

And as well as, how to validate image mime type, size, dimension, etc on laravel controller by using laravel validation rules.

Here you need to create two routes and two methods in controller along with a “uploads/” directory of public folder. So guys lets start Laravel 9 multiple images Upload Tutorial With Validations step by step easy way. lets start for laravel 9 multiple images uploaded with validation and save in database as well as in public or storage directory.

This multiple images upload in the tutorial will create an image upload form in laravel 9 with validation, which is used to store images in the database and storage directory.

This tutorial will work with Laravel versions 5, 6, 7, and 8. When syntax is different across versions, the different syntax will be demonstrated.

Let's start following example:

Step 1: Download Laravel

Let us begin the tutorial by installing a new laravel application. if you have already created the project, then skip following step.

composer create-project laravel/laravel example-app
Step 2 : Create Controller

In this step, we will create a new ImageUploadController; in this file, we will add two method index() and store() for render view and store image logic.

Let's create ImageUploadController by following command:

php artisan make:controller MultipleUploadImageController
app/Http/Controllers/MultipleUploadImageController.php
<?php
  
namespace App\Http\Controllers;
  
use Illuminate\Http\Request;
use App\Models\Image;
  
class MultipleUploadImageController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        return view('multipleUploadImage');
    }
      
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        $request->validate([
            'images' => 'required',
            'images.*' => 'required|image|mimes:jpeg,png,jpg,gif,svg|max:2048',
        ]);
      
        $images = [];
        if ($request->images){
            foreach($request->images as $key => $image)
            {
                $imageName = time().rand(1,99).'.'.$image->extension();  
                $image->move(public_path('images'), $imageName);
  
                $images[]['name'] = $imageName;
            }
        }
  
        foreach ($images as $key => $image) {
            Image::create($image);
        }
      
        return back()
                ->with('success','You have successfully upload image.')
                ->with('images', $images); 
    }
}
Store Image in Storage Folder
$request->image->storeAs('images', $imageName);

// storage/app/images/file.png
Store Image in Public Folder
$request->image->move(public_path('images'), $imageName);

// public/images/file.png
Store Image in S3
$request->image->storeAs('images', $imageName, 's3');
Step 3 : Create Routes routes/web.php
<?php

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

/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/

Route::controller(MultipleUploadImageController::class)->group(function(){
    Route::get('multiple-image-upload', 'index');
    Route::post('multiple-image-upload', 'store')->name('image.store');
});
Step 4: Create Blade File resources/views/multipleUploadImage.blade.php
<!DOCTYPE html>
<html>
<head>
    <title>How to Multiple Image Upload in Laravel 9? - Mywebtuts.com</title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/css/bootstrap.min.css" rel="stylesheet">
</head>
<body>
<div class="container">
       
    <div class="row justify-content-center mt-5">
        <div class="col-md-11 mt-3">
  
            <h3>How to Multiple Image Upload in Laravel 9? - Mywebtuts.com</h3>
       
            @if ($message = Session::get('success'))

                <div class="alert alert-success alert-dismissible fade show" role="alert">
                    <strong>{{ $message }}</strong>
                    <button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
                </div>

                @foreach(Session::get('images') as $image)
                    <img src="images/{{ $image['name'] }}" width="250px">
                @endforeach

            @endif
          
            <form action="{{ route('image.store') }}" method="POST" enctype="multipart/form-data" class="mt-4">
                @csrf
      
                <div class="mb-3">
                    <label class="form-label" for="inputImage">Select Images:</label>
                    <input 
                        type="file" 
                        name="images[]" 
                        id="inputImage"
                        multiple 
                        class="form-control @error('images') is-invalid @enderror">
      
                    @error('images')
                        <span class="text-danger">{{ $message }}</span>
                    @enderror
                </div>
       
                <div class="mb-3">
                    <button type="submit" class="btn btn-success">Upload</button>
                </div>
           
            </form>
        </div>
    </div>
</div>
</body>
</html>
Run Laravel App:

All steps have been done, now you have to type the given command and hit enter to run the laravel app:

php artisan serve

Now, you have to open web browser, type the given URL and view the app output:

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

I hope it can help you...

#Laravel 9