10-Apr-2023
.
Admin
Hi guys,
Today i am explained to the how to use in laravel mimes validation in laravel 8 and other version in laravel. when mimes validation is used to the files and images extension to validate in laravel project.
Laravel Validation Mimes Foo,Bar,... is so easy to use.so you can just follow my step by step and learn Laravel Validation Mimes Foo,Bar,... Example.
I will simple give you example of how to use mimes validation rules like image, mimes, size, and dimensions in laravel 6, laravel 7 and laravel 8 application.The mimes validation is i am used in four way to explained in this tutorial and you are just follow in my steps.
So let's start to the example and follow to the my all step.
Step 1: Create Route
Last step to create a route in web.php file and use this code.
routes/web.php
Route::get('mimes', [MimesController::class, 'index']);
Route::post('mimes/validation', [MimesController::class, 'create'])->name('mimes.validation');
Step 2: Create a MimesController
Next you can require to the MimesController so create a MimesController in just following command through.
app/Http/Controllers/MimesController.php
Example 1: Simple Laravel Image Validation Rule
/**
* The attributes that are mass assignable.
*
* @var array
*/
public function index()
{
return view('mimes');
}
/**
* The attributes that are mass assignable.
*
* @var array
*/
public function create(Request $request)
{
$input = $request->all();
$mimes = Validator::make($input, [
'name' => 'required',
'image' => 'required|image',
'file' => 'required|file|mimes:ppt,pptx,doc,docx,pdf,xls,xlsx|max:204800',
'video' => 'mimes:m4v,avi,flv,mp4,mov',
]);
dd($mimes);
}
Example 2: Laravel Validation with mimes
/**
* The attributes that are mass assignable.
*
* @var array
*/
public function index()
{
return view('mimes');
}
/**
* The attributes that are mass assignable.
*
* @var array
*/
public function create(Request $request)
{
$input = $request->all();
$mimes = Validator::make($input, [
'name' => 'required',
'image' => 'required|mimes:png,jpeg,gif',
'file' => 'required|file|mimes:ppt,pptx,doc,docx,pdf,xls,xlsx|max:204800',
'video' => 'mimes:m4v,avi,flv,mp4,mov',
]);
dd($mimes);
}
Example 3: Laravel Validation with size
/**
* The attributes that are mass assignable.
*
* @var array
*/
public function index()
{
return view('mimes');
}
/**
* The attributes that are mass assignable.
*
* @var array
*/
public function create(Request $request)
{
$input = $request->all();
$mimes = Validator::make($input, [
'name' => 'required',
'image' => 'image|size:2048|dimensions:min_width=200,min_height=200,max_width=600,max_height=600',
'file' => 'required|file',
'video' => 'mimes:m4v,avi,flv,mp4,mov',
]);
dd($mimes);
}
Example 4: Laravel File Validation with Video mimes
/**
* The attributes that are mass assignable.
*
* @var array
*/
public function index()
{
return view('mimes');
}
/**
* The attributes that are mass assignable.
*
* @var array
*/
public function create(Request $request)
{
$input = $request->all();
$mimes = Validator::make($input, [
'name' => 'required',
'image' => 'image|size:2048|dimensions:ratio=3/2',
'file' => 'required|file',
'video' => 'mimes:m4v,avi,flv,mp4,mov',
]);
dd($mimes);
}
Step 3: Create a Users Blade File
Next you can require to the users.blade.php so create a users.blade.php in just following command through.
resources/views/users.blade.php
<!DOCTYPE html>
<html>
<head>
<title>Laravel Validation Mimes Foo,Bar,... Example</title>
<link rel="stylesheet" type="text/css" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
</head>
<body>
<div class="container">
<h2>Laravel Validation Mimes Foo,Bar,... Example</h2>
<form method="POST" action="{{ route('mimes.validation') }}" autocomplete="off" enctype="multipart/form-data">
<input type="hidden" name="_token" value="{{ csrf_token() }}">
@if(count($errors))
<div class="alert alert-danger">
<strong>Whoops!</strong> There were some problems with your input.
<br/>
<ul>
@foreach($errors->all() as $error)
<li>{{ $error }}</li>
@endforeach
</ul>
</div>
@endif
<div class="row">
<div class="col-md-12">
<div class="form-group {{ $errors->has('firstname') ? 'has-error' : '' }}">
<label for="firstname">First Name:</label>
<input type="text" id="firstname" name="firstname" class="form-control" placeholder="Enter First Name" value="{{ old('firstname') }}">
<span class="text-danger">{{ $errors->first('firstname') }}</span>
</div>
</div>
<div class="col-md-12">
<div class="form-group {{ $errors->has('avatar') ? 'has-error' : '' }}">
<label for="avatar">Avatar</label>
<input type="file" id="avatar" name="avatar" class="form-control" placeholder="Enter Avatar" value="{{ old('avatar') }}">
<span class="text-danger">{{ $errors->first('avatar') }}</span>
</div>
</div>
<div class="col-md-12">
<div class="form-group {{ $errors->has('file') ? 'has-error' : '' }}">
<label for="file">File</label>
<input type="file" id="file" name="file" class="form-control" placeholder="Enter File" value="{{ old('file') }}">
<span class="text-danger">{{ $errors->first('file') }}</span>
</div>
</div>
<div class="col-md-12">
<div class="form-group {{ $errors->has('video') ? 'has-error' : '' }}">
<label for="video">Video</label>
<input type="file" id="video" name="video" class="form-control" placeholder="Enter video" value="{{ old('video') }}">
<span class="text-danger">{{ $errors->first('video') }}</span>
</div>
</div>
</div>
<div class="form-group">
<button class="btn btn-success">Submit</button>
</div>
</form>
</div>
</body>
</html>
So, finally we are done with our code we can get below output.
php artisan serve
http://localhost:8000/mimes
#Laravel 8
#Laravel 7
#Laravel
#Laravel 6