How to validate Laravel table fields in 3 new steps

Tables have always played an essential role in applications development. Laravel Schema builder façade allows easy and quick creation of tables through database migrations. However, we should validate input fields before storing data in controller functions. Similarly, new database tables also require validations. The exist keyword can validate Laravel table structures and input fields.

The Exists Rule

The exists keyword will validate data if a specific database table contains a record of a specific column value that matches a request’s attribute value.

To validate post data, you can use the built in Laravel modifier, the exists rule to check if an email exists in customers table along with other validations.

if (Customers::where('email', $cust_email )->exists()) {
    // your code...
}

The keyword helps find if a record exists in a table. The code above shows an if condition, which checks if a customer’s email is present in the id column in the Customers table.

We can validate, table columns and records in different ways. Laravel provides this new method i.e. exists, which allows you to check if your query returns any records rather than using the count method. There are few steps to follow to use exists for table validation.

Steps 1: Create new Routes

use App\Http\Controllers\ValidateController;

Route::get('exists',[ValidateController::class,'existsValidate']);
Route::post('save-exists',[ValidateController::class,'existsSave'])->name('save.exists');

This adds two routes to get and post data using the exists rule. The get method is to display the blade view while existsSave, post method saves in data entered through the blade view.

Step 2: Create new Controller

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class ValidateController extends Controller
{
    public function existsValidate()
    {
        return view('validation.existsValidate');
    }

    public function existsSave(Request $request)
    {
        $request->validate([
            'customer' => 'required|exists:cust,name',
        ]);
    }
}

The existsValidate method returns the validation.existsValidate view, and existsSave method reads a request from the view, to enter data. It validates the customer name before saving it to database table.

Step 3: Create the blade file

<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</strong></h3>
                    </div>
                    <div class="card-body">
                        <form action="{{ route('save.exists') }}" method="post">
                            @csrf
                            <div class="form-group">
                                <label>Customer Name:</label>
                                <input class="form-control" name="customer" value="{{ old('customer') }}">
                                @if($errors->has('customer'))
                                    <span class="text-danger">{{ $errors->first('customer') }}</span>
                                @endif
                            </div>
                            <div class="form-group">
                                <button class="btn btn-success btn-sm">Save</button>
                            </div>
                        </form>
                    </div>
                </div>
            </div>
        </div>
    </div>
</body>

Form data is passed to the save.exists route where customer name is read through the existsSave method. The $errors directive is used here to display any error messages post validation using the exists keyword.