Skip to content

All topics  /  Laravel

Laravel Exists Validation Example

Laravel ·

Teams applying “Laravel Exists Validation Example” in a production project can use the official resource to record implementation, review and debugging time, then compare that effort with tests and shipped functionality instead of treating activity as performance by itself.

Before copying the example into a live application, confirm version-specific behaviour with the official Laravel website and test it against the project’s actual database and runtime.

Hi Guys,

In this blog, I will show you how to validate exists() in laravel application. We will use simple use rules of laravel validator i want to check username exists on table in laravel app. This article will give you laravel exists valdation example.

The field under validation must exist in a given database table. You may explicitly specify the database column name that should be used by the validation rule by placing it after the database table name:

Here, i will show you how to works laravel validation for exists. you can see laravel validation exists on database table. This article will give you simple example of exists in laravel validation. So, let's follow few step to create example of laravel exists Validation Example.

Solution


$request->validate([
    'username' => 'required|exists:users,name',
]);

If you would like to customize the query executed by the validation rule, you may use the Rule class to fluently define the rule. In this example, we'll also specify the validation rules as an array instead of using the | character to delimit them:

Solution With Rule

use Illuminate\Support\Facades\Validator;
use Illuminate\Validation\Rule;
$validator = Validator::make($request->all(), [
    'name' => [
        'required',
        Rule::exists('users')->where(function ($query) {
            $query->where('id', 1);
        }),
    ],
]);

Full Example

Add Route

use App\Http\Controllers\ValidationController;
Route::get('exists',[ValidationController::class,'existsValidation']);
Route::post('store-exists',[ValidationController::class,'existsStore'])->name('store.exists');

Controller : app/Http/Controllers/ValidationController.php

<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class ValidationController extends Controller
{
    public function existsValidation()
    {
        return view('validation.existsValidation');
    }
    public function existsStore(Request $request)
    {
        $request->validate([
            'username' => 'required|exists:users,name',
        ]);
    }
}

View : resources/views/validation/lteValidation.php

<!DOCTYPE html>
<html>
<head>
    <title>Laravel Exists Validation Example</title>
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.5.3/css/bootstrap.min.css" integrity="sha512-oc9+XSs1H243/FRN9Rw62Fn8EtxjEYWHXRvjS43YtueEewbS6ObfXcJNyohjHqVKFPoXXUxwc+q1K7Dee6vv9g==" crossorigin="anonymous" />
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.5.1/jquery.min.js" integrity="sha512-bLT0Qm9VnAYZDflyKcBaQ2gg0hSYNQrJ8RilYldYQ1FxQYoCLtUjuuRuZo+fjqhx/qtq/1itJ0C2ejDxltZVFg==" crossorigin="anonymous"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.5.3/js/bootstrap.min.js" integrity="sha512-8qmis31OQi6hIRgvkht0s6mCOittjMa9GMqtK9hes5iEQBQE/Ca6yGE5FsW36vyipGoWQswBj/QBm2JR086Rkw==" crossorigin="anonymous"></script>
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-datepicker/1.9.0/css/bootstrap-datepicker.min.css" integrity="sha512-mSYUmp1HYZDFaVKK//63EcZq4iFWFjxSL+Z3T/aCt4IO9Cejm03q3NKKYN6pFQzY0SBOr8h+eCIAZHPXcpZaNw==" crossorigin="anonymous" />
    <script src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-datepicker/1.9.0/js/bootstrap-datepicker.min.js" integrity="sha512-T/tUfKSV1bihCnd+MxKD0Hm1uBBroVYBOYSk1knyvQ9VyZJpc/ALb4P0r6ubwVPSGB2GvjeoMAJJImBG12TiaQ==" crossorigin="anonymous"></script>
</head>
<body>
    <div class="container mt-5">
        <div class="row">
            <div class="col-md-8 offset-2 mt-5">
                <div class="card">
                    <div class="card-header bg-info text-white">
                        <h3><strong>Laravel Exists Validation Example</strong></h3>
                    </div>
                    <div class="card-body">
                        <form action="{{ route('store.exists') }}" method="post">
                            @csrf
                            <div class="form-group">
                                <label>Username :</label>
                                <input class="form-control" name="username" value="{{ old('username') }}">
                                @if($errors->has('username'))
                                    <span class="text-danger">{{ $errors->first('username') }}</span>
                                @endif
                            </div>
                            <div class="form-group">
                                <button class="btn btn-success btn-sm">Save</button>
                            </div>
                        </form>
                    </div>
                </div>
            </div>
        </div>
    </div>
</body>
</html>

Run Your Project

Now we are ready to run our example so run bellow command for quick run:

php artisan serve

Now you can open bellow URL on your browser:

http://localhost:8000/exists

It will help you....