In this tutorial you will learn about the How to Add Exists Validation in Laravel 8 Input Field and its application with practical example.
In this How to Add Exists Validation in Laravel 8 Input Field tutorial, we will learn how to add already exists validation in the laravel form. In this tutorial you will learn to use already exists validation rules in laravel form input field. In this article I will share example to use already exists validation rules in laravel form. We will implement server side validation for already exists validation rules in laravel. Suppose if you want to check the student name already exists in database table before you want to save it in database. In this case we can use exists validation rule to check if student with given name is already exists in database.
How to Add Exists Validation in Laravel 8 Input Field
In this step by step tutorial I will demonstrate you with example how to add and use exists validation rules in laravel forms. Please follow the instruction given below:
- Step 1: Create Laravel Project
- Step 2: Add Database Credentials
- Step 3: Build Model and Migration
- Step 4: Run Database Migration
- Step 5: Create Controller File
- Step 6: Register New Routes
- Step 7: Create Blade View File
- Step 8: Test Laravel App
Create Laravel Project
First of all we need to create a fresh laravel project, download and install Laravel 8 using the below command
1 |
composer create-project laravel/laravel --prefer-dist laravel-demo |
Add Database Credentials
Now, lets create a MySQL database and connect it with laravel application. After creating database we need to set database credential in application’s .env file.
1 2 3 4 5 6 |
DB_CONNECTION=mysql DB_HOST=127.0.0.1 DB_PORT=3306 DB_DATABASE=db_name DB_USERNAME=db_username DB_PASSWORD=db_password |
Build Model and Migration
Now, in this step we will create model and migration file. Please run the following command:
1 |
php artisan make:model Student -m |
Now, open app/Student.php model file and put the following code in it:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
<?php namespace App\Models; use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Model; class Student extends Model { use HasFactory; public $fillable = [ 'student_name', ]; } |
Open create_students_table.php migration file and update the function up() method as following:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
<?php use Illuminate\Database\Migrations\Migration; use Illuminate\Database\Schema\Blueprint; use Illuminate\Support\Facades\Schema; class CreateStudentsTable extends Migration { /** * Run the migrations. * * @return void */ public function up() { Schema::create('students', function (Blueprint $table) { $table->id(); $table->string('student_name'); $table->timestamps(); }); } /** * Reverse the migrations. * * @return void */ public function down() { Schema::dropIfExists('students'); } } |
Run Database Migration
Now, run the migration to create database table using following artisan command:
1 |
php artisan migrate |
Create Controller File
Now, lets create a controller named StudentController using command given below –
1 |
php artisan make:controller StudentController |
Now, add the code in the app/Http/Controller/StudentController.php.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
<?php namespace App\Http\Controllers; use Illuminate\Http\Request; use App\Models\Student; class StudentController extends Controller { public function index() { return view('welcome'); } public function store(Request $request) { $validator = $this->validate($request, [ 'student_name' => 'required|exists:students,created_at' ]); if($validator) { Student::create($request->all()); } return back()->with('success', 'Student has been added.'); } } |
Register New Routes
After this, we need to define routes in “routes/web.php” file. Lets open “routes/web.php” file and add the following routes in it.
routes/web.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
<?php use Illuminate\Support\Facades\Route; use App\Http\Controllers\StudentController; /* |-------------------------------------------------------------------------- | API Routes |-------------------------------------------------------------------------- | | Here is where you can register API routes for your application. These | routes are loaded by the RouteServiceProvider within a group which | is assigned the "api" middleware group. Enjoy building your API! | */ Route::get('/', [StudentController::class, 'index']); Route::post('form', [StudentController::class, 'store'])->name('student.validate'); |
Create Blade View File
In this step we will create a blade view file, open the resources/views/welcome.blade.php file and put the following code in it:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>Laravel Exists Validation Example</title> <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/css/bootstrap.min.css" rel="stylesheet"> </head> <body> <div> <form action="{{ route('student.validate') }}" method="post"> @csrf <div> <input class="form-control" name="student_name" value="{{ old('student_name') }}"> @if($errors->has('student_name')) <span class="text-danger">{{ $errors->first('student_name') }}</span> @endif </div> <div class="mt-2"> <button class="btn btn-primary">Save</button> </div> </form> </div> </body> </html> |
Run Development Server
Now we are ready to run our example so lets start the development server using following artisan command –
1 |
php artisan serve |
Now, open the following URL in browser to see the output –
1 |
http://127.0.0.1:8000 |