Laravel 11 Image Upload Example

Uploading and validating an image in Laravel 11 is a very basic task, To do it, you have to create an image upload form on the blade file where the image can be selected by the user and after that, you have to create a controller and route where the handle image upload with validation.

Today in this guide, we will create an image upload form and create controllers and routes to handle the image upload process to the folder and DB in the Laravel application with validation.

Laravel 11 Image Upload Example

Steps for image upload in laravel 11 applications with validation:

Step 1 – Set up New Laravel Application

Simply run composer create-project --prefer-dist laravel/laravel myBlog command on cmd to terminal window to set up new laravel application into your system:

composer create-project --prefer-dist laravel/laravel myBlog

Step 2 – Define Image Upload Routes

Defining routes to display the image upload form and send it to the controller to upload the image, open the web.php file from the routes folder, and you can define routes like the following in it:

use App\Http\Controllers\ImageUploadController;

Route::get('image-form', [ImageUploadController::class, 'index']);
Route::post('upload', [ImageUploadController::class, 'upload']);

Step 3 – Create Image Upload Controller

Create a controller file, which is used to handle image uploading in Laravel, simply run the php artisan make:controller ImageUploadController command on a CMD or terminal window:

php artisan make:controller ImageUploadController

Create two methods in the ImageUploadController.php file to handle the image upload process with validation, and write the logical code for uploading the image in it, as you can see here:

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;


class ImageUploadController extends Controller
{
    public function index()
    {
        return view('image-form');
    }

    public function upload(Request $request)
    {

        //validate image before upload
        $validatedData = $request->validate([
         'image' => 'required|image|mimes:jpg,png,jpeg,gif,svg|max:2048',
        ]);

        $name = $request->file('image')->getClientOriginalName();

        //upload image in public storage folder
        $path = $request->file('image')->store('public/images');

        return redirect('image-form')->with('status', 'Image Has been uploaded');

    }
}

Step 4 – Create Image Upload Form

Simply create an image-form.blade.php file in the resource/views folder and create an image upload form in the blade views that users can select to upload images:

<!DOCTYPE html>
<html>
<head>
  <title>Laravel 11 Image Upload Example - Tutsmake.com</title>

  <meta name="csrf-token" content="{{ csrf_token() }}">

  <link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-QWTKZyjpPEjISv5WaRU9OFeRpok6YctnYmDr5pNlyT2bRjXh0JMhjY6hW+ALEwIH" crossorigin="anonymous">

</head>
<body>

<div class="container mt-4">

  @if(session('status'))
    <div class="alert alert-success">
        {{ session('status') }}
    </div>
  @endif

  <div class="card">

    <div class="card-header text-center font-weight-bold">
      <h2>Laravel 11 Image Upload Example - Tutsmake.com</h2>
    </div>

    <div class="card-body">
        <form method="POST" enctype="multipart/form-data" id="upload-image" action="{{ url('upload') }}" >
           @csrf
            <div class="row">

                <div class="col-md-8">
                    <div class="form-group">
                        <input type="file" name="image" placeholder="Choose image" id="image">
                    @error('image')
                        <div class="alert alert-danger mt-1 mb-1">{{ $message }}</div>
                    @enderror
                    </div>
                </div>

                <div class="col-md-8">
                    <button type="submit" class="btn btn-primary" id="submit">Submit</button>
                </div>
            </div>
        </form>

    </div>

  </div>

</div>
</body>
</html>

Step 5 – Run and Test Application

Run the php artisan server command to start and test application:

php artisan serve

Open browser with http://127.0.0.1:8000/image-form URL:

http://127.0.0.1:8000/image-form

Conclusion

We hope that through this guide you learned how to upload and validate images in Laravel 11 application. Even after this, if you have any doubts regarding the image upload in laravel then you can comment to us.

Recommended Guides

AuthorDevendra Dode

Greetings, I'm Devendra Dode, a full-stack developer, entrepreneur, and the proud owner of Tutsmake.com. My passion lies in crafting informative tutorials and offering valuable tips to assist fellow developers on their coding journey. Within my content, I cover a spectrum of technologies, including PHP, Python, JavaScript, jQuery, Laravel, Livewire, CodeIgniter, Node.js, Express.js, Vue.js, Angular.js, React.js, MySQL, MongoDB, REST APIs, Windows, XAMPP, Linux, Ubuntu, Amazon AWS, Composer, SEO, WordPress, SSL, and Bootstrap. Whether you're starting out or looking for advanced examples, I provide step-by-step guides and practical demonstrations to make your learning experience seamless. Let's explore the diverse realms of coding together.

Leave a Reply

Your email address will not be published. Required fields are marked *