Step 1: Install Intervention Image Package

Run the following command:

composer require intervention/image

Laravel automatically discovers the service provider, so no need to manually add it in config/app.php.

Step 2: Publish Configuration (Optional)

If you want to customize the config:

php artisan vendor:publish --provider="Intervention\Image\ImageServiceProviderLaravelRecent"

This will create config/image.php.

Step 3: Database Setup

For multiple images, we usually have two tables:

  1. posts (or main table)
  2. post_images (to store multiple images)

Migration Example

Posts Table:

Schema::create('posts', function (Blueprint $table) {
$table->id();
$table->string('title');
$table->timestamps();
});

Post Images Table:

Schema::create('post_images', function (Blueprint $table) {
$table->id();
$table->foreignId('post_id')->constrained()->onDelete('cascade');
$table->string('image');
$table->timestamps();
});

Step 4: Models

Post.php

use Illuminate\Database\Eloquent\Model;

class Post extends Model
{
protected $fillable = ['title'];

public function images()
{
return $this->hasMany(PostImage::class);
}
}

PostImage.php

use Illuminate\Database\Eloquent\Model;

class PostImage extends Model
{
protected $fillable = ['post_id', 'image'];

public function post()
{
return $this->belongsTo(Post::class);
}
}

Step 5: Blade Form

<form action="{{ route('posts.store') }}" method="POST" enctype="multipart/form-data">
@csrf
<label>Title:</label>
<input type="text" name="title" required><label>Upload Images:</label>
<input type="file" name="images[]" multiple required>

<button type="submit">Submit</button>
</form>

Use name="images[]" and multiple attribute for multiple file uploads.

Step 6: Store Method (Insert Multiple Images)

use Illuminate\Support\Facades\Storage;
use Intervention\Image\Facades\Image;public function store(Request $request)
{
$request->validate([
'title' => 'required|string|max:255',
'images.*' => 'required|image|mimes:jpeg,png,jpg,gif|max:2048',
]);

$post = Post::create([
'title' => $request->title
]);

if ($request->hasFile('images')) {
foreach ($request->file('images') as $image) {
$filename = time() . '_' . $image->getClientOriginalName();

// Resize image
$img = Image::make($image)->resize(800, 600, function ($constraint) {
$constraint->aspectRatio();
$constraint->upsize();
})->encode();

Storage::put("public/images/{$filename}", $img);

// Save to post_images table
$post->images()->create([
'image' => "images/{$filename}"
]);
}
}

return redirect()->back()->with('success', 'Post and images uploaded successfully!');
}

Step 7: Update Method (Update Multiple Images)

  1. You can replace all images or add new ones.
  2. Delete old images if replacing.
public function update(Request $request, $id)
{
$request->validate([
'title' => 'required|string|max:255',
'images.*' => 'nullable|image|mimes:jpeg,png,jpg,gif|max:2048',
]);$post = Post::findOrFail($id);
$post->title = $request->title;
$post->save();

if ($request->hasFile('images')) {
// Delete old images if you want to replace
foreach ($post->images as $img) {
if (Storage::exists('public/' . $img->image)) {
Storage::delete('public/' . $img->image);
}
$img->delete();
}

// Save new images
foreach ($request->file('images') as $image) {
$filename = time() . '_' . $image->getClientOriginalName();

$img = Image::make($image)->resize(800, 600, function ($constraint) {
$constraint->aspectRatio();
$constraint->upsize();
})->encode();

Storage::put("public/images/{$filename}", $img);

$post->images()->create([
'image' => "images/{$filename}"
]);
}
}

return redirect()->back()->with('success', 'Post updated successfully!');
}

Step 8: Display Multiple Images

<h3>{{ $post->title }}</h3>
@foreach($post->images as $img)
<img src="{{ asset('storage/' . $img->image) }}" width="200" style="margin:10px;">
@endforeach

Step 9: Routes

Route::get('/posts/create', [PostController::class, 'create'])->name('posts.create');
Route::post('/posts/store', [PostController::class, 'store'])->name('posts.store');
Route::get('/posts/{id}/edit', [PostController::class, 'edit'])->name('posts.edit');
Route::post('/posts/{id}/update', [PostController::class, 'update'])->name('posts.update');

Categorized in:

Laravel,