This content originally appeared on DEV Community and was authored by Saddam Hossain
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
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
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);
}
}
This content originally appeared on DEV Community and was authored by Saddam Hossain
Saddam Hossain | Sciencx (2024-10-16T03:19:14+00:00) Laravel 11 Image Validation Rules – Complete Example and Guide. Retrieved from https://www.scien.cx/2024/10/16/laravel-11-image-validation-rules-complete-example-and-guide/
Please log in to upload a file.
There are no updates yet.
Click the Upload button above to add an update.