DEV Community

Cover image for Laravel 11 Image Validation Rules – Complete Example and Guide
Saddam Hossain
Saddam Hossain

Posted on

1

Laravel 11 Image Validation Rules – Complete Example and Guide

Discover how to implement image validation rules in Laravel 11 with this comprehensive example. Learn how to validate image uploads, set file size limits, file types, dimensions, and more. This step-by-step guide is perfect for developers looking to ensure secure and efficient image handling in their Laravel 11 applications. You Can Learn Laravel 11: How to Remove Public from URL – Complete Guide with Example

Step for Laravel 11 Validation Rules for Image and Photo

Step 1: Install Laravel 11

This step is not required; however, if you have not created the Laravel app, then you may go ahead and execute the below command:

composer create-project laravel/laravel ImageValidation
Enter fullscreen mode Exit fullscreen mode

Step 2: Create Controller

In this step, we will create a new ImageController; in this file, we will add two method index() and store() for render view and store image logic. You Can Learn How to Add Text to Image in Laravel 11 – Step-by-Step Guide

Let’s create ImageController by following command:

php artisan make:controller ImageController
Enter fullscreen mode Exit fullscreen mode

next, let’s update the following code to Controller File.

app/Http/Controllers/ImageController.php

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\View\View;
use Illuminate\Http\RedirectResponse;

class ImageController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(): View
    {
        return view('imageUpload');
    }

    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request): RedirectResponse
    {
        $this->validate($request, [
            'image' => [
                        'required',
                        'image',
                        'mimes:jpg,png,jpeg,gif,svg',
                        'dimensions:min_width=100,min_height=100,max_width=1000,max_height=1000',
                        '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); 
    }
}
Enter fullscreen mode Exit fullscreen mode

READ MORE

Do your career a big favor. Join DEV. (The website you're on right now)

It takes one minute, it's free, and is worth it for your career.

Get started

Community matters

Top comments (0)

Cloudinary image

Video API: manage, encode, and optimize for any device, channel or network condition. Deliver branded video experiences in minutes and get deep engagement insights.

Learn more

👋 Kindness is contagious

Dive into an ocean of knowledge with this thought-provoking post, revered deeply within the supportive DEV Community. Developers of all levels are welcome to join and enhance our collective intelligence.

Saying a simple "thank you" can brighten someone's day. Share your gratitude in the comments below!

On DEV, sharing ideas eases our path and fortifies our community connections. Found this helpful? Sending a quick thanks to the author can be profoundly valued.

Okay