Laravel 10 Image Upload Code Example

Feb 20, 2023 . Admin



Hi friends,

I'll walk you through the Laravel 10 image upload process in this article. How to upload an image in Laravel 10 step by step with validation. Laravel 10 will be used to upload photographs and show them. This post will provide you with a straightforward example of validating an image upload into a database using Laravel 10. Create an image upload form in Laravel 10 by following the tutorial steps below.

And as well as, how to validate image mime type, size, dimension, etc on laravel controller by using laravel validation rules. This Image upload in the tutorial will create an image upload form in laravel 10 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 : Add 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); 
    }
}
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 : Add Routes routes/web.php
name('image.store');
Step 4: Add Blade File resources/views/imageUpload.blade.php
<!DOCTYPE html>
<html>
<head>
    <title>Laravel 10 Image Upload Step by Step Example - </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 10 Image Upload Step by Step Example - </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>
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/upload-image
Output:

I hope it can help you...

#Laravel 10