Here we will see how to validate before or equal date validation in laravel application. We will talk about laravel before or equal date validation example.

The field under validation must be a value preceding or equal to the given date. The dates will be passed into the PHP strtotime function in order to be converted into a valid DateTime instance. In addition, like the after rule, the name of another field under validation may be supplied as the value of date.

Given below is the solution and full example for laravel before or equal date validation example.



Solution-1:

$request->validate([

    'start_date' => 'required',

    'end_date' => 'required|date|before_or_equal:start_date'

]);




Solution 2 : Other Date Format

$request->validate([

    'start_date' => 'required',

    'end_date' => 'required|date_format:d/m/Y|before_or_equal:start_date'

]);




Full Example :

Add Route

use App\Http\Controllers\ValidationController;

Route::get('before-or-equal-date',[ValidationController::class,'beforeOrEqualDate']);
Route::post('store-or-before-equal-date',[ValidationController::class,'beforeOrEqualDateStore'])->name('store-before-or-equal-date');




Controller : app/Http/Controllers/ValidationController.php

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class ValidationController extends Controller
{
    public function beforeOrEqualDate()
    {
        return view('validation.beforeEqualDate');
    }

    public function beforeOrEqualDateStore(Request $request)
    {
        $request->validate([
            'start_date' => 'required',
            'end_date' => 'required|date|before:start_date'
        ]);
    }
}




View : resources/views/validation/beforeEqualDate.blade.php

<!DOCTYPE html>
<html>
<head>
    <title>Laravel before_or_equal Date 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 before_or_equal Date Validation Example</strong></h3>
                    </div>
                    <div class="card-body">
                        <form action="{{ route('store-before-or-equal-date') }}" method="post">
                            @csrf
                            <div class="form-group">
                                <label>Start Date :</label>
                                <input class="datepicker form-control" name="start_date" value="{{ old('start_date') }}">
                                @if($errors->has('start_date'))
                                    <span class="text-danger">{{ $errors->first('start_date') }}</span>
                                @endif
                            </div>
                            <div class="form-group">
                                <label>End Date :</label>
                                <input class="datepicker form-control" name="end_date" value="{{ old('end_date') }}">
                                @if($errors->has('end_date'))
                                    <span class="text-danger">{{ $errors->first('end_date') }}</span>
                                @endif
                            </div>
                            <div class="form-group">
                                <button class="btn btn-success btn-sm">Save</button>
                            </div>
                        </form>
                    </div>
                </div>
            </div>
        </div>
    </div>
    <script type="text/javascript">
        $('.datepicker').datepicker({
            autoclose: true
        });
                $('.end-date').datepicker({
            autoclose: true
        });
    </script>
</body>
</html>




Run Your Project

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

php artisan serve

Now you can open this URL below on your browser:

http://localhost:8000/before-or-equal-date


I hope this example helps you.