Laravel Checking If a Record Exists
660,057
Solution 1
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
}
Solution 2
if (User::where('email', Input::get('email'))->exists()) {
// exists
}
Solution 3
In laravel eloquent, has default exists()
method, refer followed example.
if (User::where('id', $user_id )->exists()) {
// your code...
}
Solution 4
One of the best solution is to use the firstOrNew
or firstOrCreate
method. The documentation has more details on both.
Solution 5
if($user->isEmpty()){
// has no records
}
Eloquent uses collections. See the following link: https://laravel.com/docs/5.4/eloquent-collections
Author by
Ben
Updated on July 08, 2022Comments
-
Ben almost 2 years
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? -
Volatil3 over 8 yearsif you call
exists()
against a non existent record then it gives error:Call to a member function exists() on null
-
lukasgeiter over 8 years@Volatil3 You are doing something wrong. You can't call exists after you've already ran the query
-
Volatil3 over 8 years@lukasgeiter guess you are right. I had already called
first()
-
Combine over 8 yearsI used to use -> $email_exists = DB::table('users')->select('email')->where('email', $request->email)->count(); but your method is better :)
-
jshbrmn over 8 yearsalternatively,
if (!User::where('email', '=', Input::get('email'))->exists())...
could work -
Janaka Pushpakumara about 8 yearsI think this is the better way to find if User is exists. User::where('email', '=', 'value')->count() > 0;
-
Gokigooooks about 8 yearswhile not fitting the question, still, very useful functions. the difference between the two is that firstOrNew instantiates an instance of the model called while firstOrCreate saves the queried model instantly so you need to update changes on the firstOrCreate'd model.
-
TrueStory about 7 yearsIf you have a table User with ...say over1,000,000,000 records, you'll be checking for a veeeeeeery long time
-
Pathros over 6 years@JanakaPushpakumara That alone has sometimes caused me error. I also add
&& isset()
-
Agil over 6 yearsexists() might be a good option too but I feel like it is not the most optimised way, is it?
-
Pezhvak over 6 years@Volatil3 i just tested ->exists() with laravel 5.5, it says false if it doesn't exist.
-
Roark about 6 yearsFYI, you can leave out the '=' value:
User::where('email', Input::get('email'))->exists();
-
user3574492 almost 6 yearsYeah but its not returning a collection. It's returning a single model object as you would assume each user has a unique
email
so->isEmpty()
will throw an error. -
Pathros over 5 yearsAnd what if I get the following error:
Call to a member function isEmpty() on null
-
JustAMartin over 5 yearsWhy making SQL to continue counting rows with
count
after it has already found one?Exists
is more appropriate here - it stops right after finding one match. Even if performance difference might be negligible, still it expresses the intention better. -
umarbilal over 5 yearsWhat if you want to check for multiple row exist?
-
Edgar Ortega about 5 yearsYou could replace
find
withwhere
.User::where(id, 1)->first()
-
William Turrell almost 5 yearsYep, or another way of thinking of it is, use firstOrCreate if you can pass all the attributes in one go (use the second parameter), but firstOrNew if you're going to need further logic before saving.
-
Peter Wolf almost 5 yearsLink only answers are not good answers. See "How do I write a good answer?"
-
Robo Robok over 4 yearsThis should be the accepted answer. The most efficient and dedicated way to do it is through the
exists()
method. -
BonisTech over 3 years@Pezhvak Same here with laravel 7.x
-
AliN11 over 3 yearsNote that by using
count
, you are counting a certain record throughout the table. While theexists
method returnstrue
if the table contains at least one desired record. -
AliN11 over 3 yearsPlease consider that
count
method counts records and it is not good performance-wise in this scenario. -
AliN11 over 3 yearsFetching all records, then counting?!
-
simpson over 2 yearsin laravel 8.x ;exists() returns false if no row found.so it is safe to use.
-
Inderjeet over 2 yearsNot working with milliseconds case. Is there any way to fix this?
-
Hamid Teimouri over 2 years@AliN11 I did not use
count
method