Laravel 8 Validation With All Mimes Tutorial Example

Jul 15, 2021 . Admin

Hello Friends,

Now let's see example of how to use validation mimes in laravel. This is a short guide on laravel if validation mimes. We will use how to use validation mimes in laravel. Here you will learn how to use validation mimes in laravel. Let's get started with how to validation mimes in laravel.

Here i will give you many example how you can validation mimes in laravel.

Step 1 : Install Laravel 8

we need to get fresh laravel 8 version application So let's open terminal and run bellow command to install fresh laravel project.

composer create-project --prefer-dist laravel/laravel blog
Step 2 : Add Route

Now, we will add routes so add bellow route in your routes.php file.

Path: routes\web.php

<?php
    use App\Http\Controllers\DocumentController;

    /*
    |--------------------------------------------------------------------------
    | Web Routes
    |--------------------------------------------------------------------------
    |
    | Here is where you can register web routes for your application. These
    | routes are loaded by the RouteServiceProvider within a group which
    | contains the "web" middleware group. Now create something great!
    |
    */

        Route::get('documents', [DocumentController::class, 'index']);
        Route::post('documents/validation', [DocumentController::class, 'create'])->name('documents.validation');
    });
?>
Step 3 : Create Controller
php artisan make:controller DocumentController

Path: app\Http\Controllers\DocumentController.php

Example :

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Models\Document;
use Illuminate\Support\Facades\Validator;

class DocumentController extends Controller
{
    /**
     * Write code on Method
     *
     * @return response()
    */
    public function index()
    {
        return view('documents');
    }

    /**
     * Write code on Method
     *
     * @return response()
    */
    public function create(Request $request)
    {
        $input = $request->all();

        $documents = Validator::make($input, [
            'image' => 'required|mimes:png,jpg, jpeg, png, bmp, gif,webp,image|size:2048|dimensions:min_width=200,min_height=200,max_width=600,max_height=600,svg|max:2048',
            'document' => 'required|file|mimes:ppt,pptx,doc,docx,xlsx|max:204800,csv,txt,xlx,xls,pdf|max:2048',
            'video' => 'required|documents:m4v,avi,flv,mp4,mov,mimes:mp4,ogg | max:20000,qt | max:20000,mimetypes:video/x-ms-asf,ogx,oga,ogv,webm',
        ]);
        dd($documents);
    }
}
Step 4 : Blade File

In this last step we will create documents.blade.php file.

Path: resources\views\documents.blade.php

<!DOCTYPE html>
<html>
    <head>
        <title>Laravel 8 Validation With All Mimes Tutorial 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 Tutorial</h2>
            <form method="POST" action="{{ route('documents.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('image') ? 'has-error' : '' }}">
                            <label for="image">Image</label>
                            <input type="file" id="image" name="image" class="form-control" placeholder="Enter image" value="{{ old('image') }}">
                            <span class="text-danger">{{ $errors->first('image') }}</span>
                        </div>
                    </div>
                    <div class="col-md-12">
                        <div class="form-group {{ $errors->has('document') ? 'has-error' : '' }}">
                            <label for="document">Document</label>
                            <input type="document" id="document" name="document" class="form-control" placeholder="Enter document" value="{{ old('document') }}">
                            <span class="text-danger">{{ $errors->first('document') }}</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>

Now run bellow command for quick run:

php artisan serve

Now open bellow URL on your browser:

localhost:8000/documents

It will help you....

#Laravel 8 #Laravel