How to Multiple Image Upload in Laravel 11

Hello developers, in this article, I'll demonstrate how to perform multiple image uploads in Laravel 11. Here, we'll go through a step-by-step guide on multiple image uploads in Laravel 11. Sometimes we require multiple images at the same to upload.

Throughout this article, we'll create an images table and create migrations and models to store the images in the table.

Laravel 11 Multiple Image Upload Example

Step 1: Install Laravel 11 Application

Step 2: Create Migration and Model

Step 3: Create Controller

Step 4: Create Routes

Step 5: Create Blade File

Step 6: Run the Laravel App

 

how_to_multiple_image_upload_in_laravel_11

 

Step 1: Install Laravel 11 Application

In this step, we'll install laravel 11 application using the following composer command.

composer create-project laravel/laravel laravel-11-example

 

Step 2: Create Migration and Model

After that, we'll create a migration for the images table. So, run the following command to create a migration.

php artisan make:migration create_images_table

Migration:

<?php
  
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
  
return new class extends Migration
{
    /**
     * Run the migrations.
     */
    public function up(): void
    {
        Schema::create('images', function (Blueprint $table) {
            $table->id();
            $table->string('name');
            $table->timestamps();
        });
    }
  
    /**
     * Reverse the migrations.
     */
    public function down(): void
    {
        Schema::dropIfExists('images');
    }
};

Then, run the migration using the following command.

php artisan migrate

After migrating the table we'll create a model using the following command.

php artisan make:model Image

app/Models/Image.php

<?php
  
namespace App\Models;
  
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
  
class Image extends Model
{
    use HasFactory;
  
    protected $fillable = [
        'name'
    ];
}

 

Step 3: Create Controller

Now, we'll create an ImageContrller.php file using the following command. In this file, we'll define the multiple images store in the database.

php artisan make:controller ImageController

app/Http/Controllers/ImageController.php

<?php
      
namespace App\Http\Controllers;
    
use Illuminate\Http\Request;
use App\Models\Image;
use Illuminate\View\View;
use Illuminate\Http\RedirectResponse;
    
class ImageController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(): View
    {
        return view('image-upload');
    }
         
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request): RedirectResponse
    {
        $request->validate([
            'images' => 'required|array',
            'images.*' => 'required|image|mimes:jpeg,png,jpg|max:4096',
        ]);
  
        $images = [];
  

        foreach($request->file('images') as $image) {
            // Generate a unique name for the image
            $image_name = time() . '_' . uniqid() . '.' . $image->getClientOriginalExtension();
              
            // Move the image to the desired location
            $image->move(public_path('images'), $image_name);
  
            $images[] = ['name' => $image_name];
        }
  
        foreach ($images as $imageData) {
            Image::create($imageData);
        }
          
        return back()->with('success', 'Images uploaded successfully!'); 
    }
}

Store Images in Storage Folder

$image->storeAs('images', $image_name);
  
// storage/app/images/test.png

Store Images in Public Folder

$image->move(public_path('images'), $image_name);
  
// public/images/test.png

Store Images in S3

$image->storeAs('images', $image_name, 's3');

 

Step 4: Create Routes

In this step, we'll define routes into the web.php file.

routes/web.php

<?php
  
use Illuminate\Support\Facades\Route;
  
use App\Http\Controllers\ImageController;
  
Route::get('image-upload', [ImageController::class, 'index']);
Route::post('image-upload', [ImageController::class, 'store'])->name('image.store');
 
Step 5: Create Blade File

Then, we'll create an image-upload.blade.php file. In this file, we'll define an image upload form with a submit button.

resources/views/image-upload.blade.php

<!DOCTYPE html>
<html>
<head>
    <title>How to Multiple Image Upload in Laravel 11 - Techsolutionstuff</title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/css/bootstrap.min.css" rel="stylesheet" crossorigin="anonymous">
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.5.1/css/all.min.css" />
</head>
        
<body>
<div class="container">
  
    <div class="card mt-5">
        <h3 class="card-header p-3"><i class="fa fa-star"></i> How to Multiple Image Upload in Laravel 11 - Techsolutionstuff</h3>
        <div class="card-body">
  
            @session('success')
                <div class="alert alert-success" role="alert"> 
                    {{ $value }}
                </div>
                @foreach(Session::get('images') as $image)
                    <img src="images/{{ $image['name'] }}" width="300px">
                @endforeach
            @endsession
            
            <form action="{{ route('image.store') }}" method="POST" enctype="multipart/form-data" class="mt-2">
                @csrf
       
                <div class="mb-3">
                    <label class="form-label" for="input-image">Select Images:</label>
                    <input 
                        type="file" 
                        name="images[]" 
                        id="input-image"
                        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"><i class="fa fa-save"></i> Upload</button>
                </div>
             
            </form>
        </div>
    </div>
</div>
</body>
      
</html>
 
Step 6: Run the Laravel App

In the last step, we'll run the laravel 11 application using the following command.

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