How to check if database row exist in laravel?

Member

by brandy , in category: PHP Frameworks , 4 months ago

How to check if database row exist in laravel?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

by wilmer.lemke , 4 months ago

@brandy 

You can check if a database row exists in Laravel by using the exists() method on an Eloquent model. Here's an example:

1
2
3
4
5
6
7
8
9
use AppModelsUser;

$user = User::where('email', '[email protected]')->first();

if ($user->exists()) {
    // Row exists
} else {
    // Row does not exist
}


In this example, we are checking if a user with the email address '[email protected]' exists in the database. The exists() method will return true if the row exists, and false if it does not.