Laravel 9 Image Upload Example

Laravel 9 Image Upload Example

Hello friends, Today we will show How to image upload in laravel 9. In this example we will explain step by step to laravel 9 upload image and save to database. Here you will learn laravel image upload example, laravel image upload code, image upload laravel 9, laravel 9 image upload example. So let’s  following step to upload image in laravel.

Step 1 : 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 ImageUploadController

app/Http/Controllers/ImageUploadController.php

<?php
  
namespace App\Http\Controllers;
  
use Illuminate\Http\Request;
  
class ImageUploadController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        return view('imageUpload');
    }
      
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        $request->validate([
            'image' => 'required|image|mimes:jpeg,png,jpg,gif,svg|max:2048',
        ]);
      
        $imageName = time().'.'.$request->image->extension();  
       
        $request->image->move(public_path('images'), $imageName);
    
        /* 
            Write Code Here for
            Store $imageName name in DATABASE from HERE 
        */
      
        return back()
            ->with('success','You have successfully upload image.')
            ->with('image',$imageName); 
    }
}

Step 2 : Add Routes

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


Route::get('upload-image', [ ImageUploadController::class, 'index' ]);
Route::post('upload-image', [ ImageUploadController::class, 'store' ])->name('image.store');

Step 3: Create Blade File

<!DOCTYPE html>
<html>
<head>
    <title>Laravel 9 Image Upload Example - Web-tuts.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="panel panel-primary">

        <div class="panel-heading mt-5 text-center">
            <h2>Laravel 9 Image Upload Example - Web-tuts.com</h2>
        </div>
 
        <div class="panel-body mt-5">

            @if ($message = Session::get('success'))
                <div class="alert alert-success alert-dismissible fade show mb-2" role="alert">
                    {{ $message }}
                    <button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
                </div>
                <img src="images/{{ Session::get('image') }}" class="mb-2" style="width:400px;height:200px;">
            @endif
      
            <form action="{{ route('image.store') }}" method="POST" enctype="multipart/form-data">
                @csrf
      
                <div class="mb-3">
                    <label class="form-label" for="inputImage">Select Image:</label>
                    <input 
                        type="file" 
                        name="image" 
                        id="inputImage"
                        class="form-control @error('image') is-invalid @enderror">
      
                    @error('image')
                        <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>

Now, Go to your web browser, type the given URL and view the app output:

http://localhost:8000/upload-image

I hope you understand of laravel 9 image upload and it can help you…

Read Also :  Laravel Eloquent firstWhere() Example