I am new to Laravel. How do I find if a record exists?
$user = User::where('email', '=', Input::get('email'));
What can I do here to see if $user
has a record?
$result = User::where("email", $email)->exists(); The above clause will give true if record exists and false if record doesn't exists. So always try to use where() for record existence and not find() to avoid NULL error. Show activity on this post.
You can call eloquent count function to get count and then check if count is equal to zero. if($collection->isEmpty()){ //products table is empty. }
Check if not null: whereNotNullSELECT * FROM users WHERE last_name IS NOT NULL; The equivalent to the IS NOT NULL condition in Laravel Eloquent is the whereNotNull method, which allows you to verify if a specific column's value is not NULL .
It depends if you want to work with the user afterwards or only check if one exists.
If you want to use the user object if it exists:
$user = User::where('email', '=', Input::get('email'))->first(); if ($user === null) { // user doesn't exist }
And if you only want to check
if (User::where('email', '=', Input::get('email'))->count() > 0) { // user found }
Or even nicer
if (User::where('email', '=', Input::get('email'))->exists()) { // user found }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With