I am new to Laravel. Please excuse the newbie question but how do I find if a record exists?
<?php
$user = User::where('email', '=', Input::get('email'));
What can I do here to see if $user
has a record?
I am new to Laravel. Please excuse the newbie question but how do I find if a record exists?
<?php
$user = User::where('email', '=', Input::get('email'));
What can I do here to see if $user
has a record?
Eloquent uses collections. See the following link: https://laravel.com/docs/5.4/eloquent-collections
can be written as
This will return true or false without assigning a temporary variable if that is all you are using $user for in the original statement.
In your Controller
In your View - Display Already Exist Message
this is simple code to check email is exist or not in database
Checking for
null
withinif
statement prevents Laravel from returning 404 immediately after the query is over.It seems like it runs double query if the user is found, but I can't seem to find any other reliable solution.
One of the best solution is to use the
firstOrNew
orfirstOrCreate
method.