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

Image of AssemblyAI tool

Challenge Submission: SpeechCraft - AI-Powered Speech Analysis for Better Communication

SpeechCraft is an advanced real-time speech analytics platform that transforms spoken words into actionable insights. Using cutting-edge AI technology from AssemblyAI, it provides instant transcription while analyzing multiple dimensions of speech performance.

Read full post

Top comments (0)

👋 Kindness is contagious

Discover a treasure trove of wisdom within this insightful piece, highly respected in the nurturing DEV Community enviroment. Developers, whether novice or expert, are encouraged to participate and add to our shared knowledge basin.

A simple "thank you" can illuminate someone's day. Express your appreciation in the comments section!

On DEV, sharing ideas smoothens our journey and strengthens our community ties. Learn something useful? Offering a quick thanks to the author is deeply appreciated.

Okay