Laravel Eloquent exists() and doesntExist() Example

Laravel exists and doesntExist Query Example; In this tutorial we are going to share how to use exists query or doesntExists query in laravel application.

Many times we use count method to determine if any records exist that match your query’s constraints but incase we can use the exists and doesntExist methods. exists() and doesntExist() eloquent query return true or false value so you have to just put in your condition.

exists() query used to any rows exist for the current query and doesntExist() query used to no rows exist for the current query. You can check if records is exists or not in database with laravel 5 laravel 6, laravel 7, laravel 8 or laravel 9 version.

Example 1: Laravel Exists Query

use App\Models\User;

class UserController extends Controller
{
    public function index()
    {
        $user = User::where('is_active', 1)->exists();

        if ($user) {
            //output true
        } else {
            //output false
        }
    }
}

Example 1: Laravel doesntExists Query

use App\Models\User;

class UserController extends Controller
{
    public function index()
    {
        $user = User::where('is_active', 1)->doesntExist();

        if ($user) {
            //output true
        } else {
            //output false
        }
    }
}

I hope you enjoy with these examples ..

Leave a Comment