For getting every new tutorial link Please join our telegram group

Part 15 laravel check database record exist or not by exists() and doesntExist() method

In this laravel query builder tutorial we will discuss exist and doesntExist query methods. It check record exist or not in database tables in laravel 6, laravel 7 and laravel 8.

How to check database record exist or not in laravel.

Laravel use two type of methods for checking record exist or not in database table.

  1. exists()
  2. doesntExist()

Laravel exists() method

Laravel query builder exists() method check data exist or not in a database table is data exist then it gives true else it give false output. Let an example of exists method

<?php

namespace App\Http\Controllers;
use Illuminate\Http\Request;

class StudentController extends Controller{
 public function index(){
    $isExist = DB::table('students')->where('name', 'anmol sharma')->exists();

    if($isExist){
      echo 'Record found';
    } else{
      echo 'Record not found';
    }
  }
}

Laravel doesntExist() method

Laravel doesntExist is just opposite of exists method it also check record exist or not. If database table record exist than it give flase else it give true. Example of doesntExist method

<?php

namespace App\Http\Controllers;
use Illuminate\Http\Request;

class StudentController extends Controller{
 public function index(){
    $isExist = DB::table('students')->where('name', 'anmol sharma')->doesntExist();

    if($isExist){
      echo 'Record not found';
    } else{
      echo 'Record found';
    }
  }
}

You read this tutorial on advanced web tutorial. here we provide beginners to advanced tutorial.

php laravel developer anmol sharma

Anmol Sharma

I am a software engineer and have experience in web development technologies like php, Laravel, Codeigniter, javascript, jquery, bootstrap and I like to share my deep knowledge by these blogs.

Related tutorial links