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.
- exists()
- 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.
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
- laravel query builder introduction tutorial
- part 1 laravel query insert data
- part 2 laravel all type of where conditions in query builder
- part 3 laravel whereDate, whereYear and whereTime qyery method
- part 4 compare two db columns in laravel query builder
- part 6 laravel pluck method get single column value
- part 7 laravel select and addselect query in query builder
- part 5 laravel first and find queries
- part 8 laravel edit and update method query explaination
- Part 9 laravel query for update or insert data by one method
- part 10 laravel delete vs truncate vs drop method
- part 11 gropupby and orderby query tutorial
- Part 12 laravel union of two queries
- part 13 laravel all aggregate functions with example
- part 14 laravel query builder chunk method
- part 15 laravel exists() and doesntExist() method
- part 16 laravel paginate vs simplePaginate method
- part 17 laravel all join methods