Join WhatsApp ChannelJoin Now

Upload & Store Image in Laravel

hi Dev,
today in this article we will understand how to upload & Store Image in Laravel.

Follow a few straightforward steps to let users upload images via a Blade form and store them in Laravel’s filesystem. So let’s start :

Install Laravel 12

composer create-project laravel/laravel example-image

Create Model

php artisan create_images_table

database/migrations/create_images_table.php

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

app/Models/Image.php

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;


class Image extends Model
{
    
    protected $fillable = [
        'title',
        'image',
    ];

    
}

Create Controller

php artisan make:controller ImageController

app/Http/Controllers/ImageController.php

<?php

namespace App\Http\Controllers;
use App\Models\Image;
use Illuminate\Http\Request;

class ImageController extends Controller
{
    public function upload(Request $request)
    {
        $file = $request->file('file');
        $imgPath = time().'_'.$file->getClientOriginalName();
        $file->move(public_path('images/imglist'), $imgPath);

        $image = new Image();
        $image->path = 'images/imglist/' . $imgPath;
        $image->save();

        return redirect()->route('images.list')
                         ->with('success', 'Image uploaded successfully');
    }

    public function list()
    {
        $imageData = Image::all();
        return view('display', compact('imageData'));
    }
}

Create Add Routes

routes/web.php


use App\Http\Controllers\ImageController;

Route::get('/images', [ImageController::class, 'list'])->name('images.list');
Route::post('/upload', [ImageController::class, 'upload'])->name('images.upload');

Create Blade File

resources/views/addpost.blade.php

 <!DOCTYPE html>
<html>
<head><title>Upload Image</title></head>
<body>
  @if(session('success'))
    <p style="color: green;">{{ session('success') }}</p>
  @endif

  <form action="{{ route('images.upload') }}" method="POST" enctype="multipart/form-data">
    @csrf
    <input type="file" name="file" required>
    <button type="submit">Upload</button>
  </form>
  
  <a href="{{ route('images.list') }}">View Gallery</a>
</body>
</html>

resources/views/display.blade.php

<!DOCTYPE html>
<html>
<head><title>Image Gallery</title></head>
<body>
  <h2>Uploaded Images</h2>
  <a href="{{ url('/upload') }}">Upload more</a>
  <div style="display: flex; flex-wrap: wrap; gap: 10px; margin-top: 15px;">
    @foreach($imageData as $img)
      <div style="border: 1px solid #ccc; padding: 5px;">
        <img src="{{ asset($img->path) }}" alt="Image" style="width:150px;height:auto;">
      </div>
    @endforeach
  </div>
</body>
</html>

Run Laravel App:

php artisan serve

I hope it will assist you…

Recommended Posts