Laravel after_or_equal Date Validation Example Tutorial


Share:
Published By: Admin, Published On: Jul 26, 2021, Category: Laravel 8 Laravel

Hi Dev,

Today,In this example, i will learn you how to validate after_or_equal date validation in laravel. i will explain you after_or_equal date validation example in laravel.i will talk about laravel after_or_equal date validation example.

In laravel inbuilt function The field under validation must be a value after or equal to the given date. For more information, see the after rule. The field under validation must be a value after or equal a given date. The dates will be passed into the strtotime PHP function:

So, here i will show you start date after or equal date validation in laravel. Here i will give you solution and full example So let's see the bellow example.

Solution
$request->validate([
    'start_date' => 'required',
    'end_date' => 'required|date|after_or_equal:start_date'
]);
Add Route Path : routes/web.php
<?php

use Illuminate\Support\Facades\Route;
use App\Http\Controllers\HomeController;

/*
|--------------------------------------------------------------------------
| 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!
|
*/

// HomeController
Route::get('after-or-equal',[HomeController::class,'create'])->name('after-or-equal.create');
Route::post('after-or-equal/store',[HomeController::class,'store'])->name('after-or-equal.store');
Create HomeController

In this step, you can create a new controller as HomeController So let's open terminal run bellow artisan command:

php artisan make:controller HomeController
Path : app\Http\Controllers\HomeController
<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class HomeController extends Controller
{
    /**
     * Write code on Method
     *
     * @return response()
     */
    public function create()
    {
        return view('index');
    }
    
    /**
     * Write code on Method
     *
     * @return response()
     */
    public function store(Request $request)
    {
        $request->validate([
          'start_date' => 'required',
          'end_date' => 'required|date|after_or_equal:start_date'
        ]);

        dd('done');
    }
}

Create View file Path: resources\views\index.blade.php
<!DOCTYPE html>
<html>
<head>
    <title>Laravel after_or_equal Date Validation Example - MyWebTuts.com</title>
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.5.3/css/bootstrap.min.css"/>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.5.3/js/bootstrap.min.js"></script>
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-datepicker/1.9.0/css/bootstrap-datepicker.min.css"/>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-datepicker/1.9.0/js/bootstrap-datepicker.min.js"></script>
</head>
<body class="bg-dark">
    <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-success text-white">
                        <h5><strong>Laravel after_or_equal Date Validation Example - MyWebTuts.com</strong></h5>
                    </div>
                    <div class="card-body">
                        <form action="{{ route('after-or-equal.store') }}" method="post">
                            @csrf
                            <div class="form-group">
                                <label>Start Date :</label>
                                <input class="datepicker form-control" name="start_date" value="{{ old('start_date') }}" autocomplete="off">
                                @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') }}" autocomplete="off">
                                @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>
Output

It will help you...

Share: