Laravel Eloquent exists() and doesntExist() query Example

Admin   Laravel   249  2021-03-15 20:30:05

Hello Friend,

In this blog, I will show you exists query or doesntExists query in laravel application. We will learn laravel exists or doesntExist eloquent query.

Instead of using the countmethod to determine if any records exist that match your query's constraints, you may use the exists and doesntExist methods.

If you check users without count() then you can use exists or doesntexists query in laravel application.

exists() query used to any rows exist for the current query and doesntExist() query used to no rows exist for the current query.

Example 1

 

/**

* The attributes that are mass assignable.

*

* @var array

*/

public function index()

{

return DB::table('users')->where('is_active', 1)->exists();

}

Example 2

 

/**

* The attributes that are mass assignable.

*

* @var array

*/

public function index()

{

return DB::table('users')->where('is_active', 1)->doesntExist();

}

It will help you...