Laravel Eloquent - Get one Row
Simply use this:
$user = User::whereEmail($email)->first();
Using Laravel Eloquent you can get one row using first()
method,
it returns first row of table if where()
condition is not found otherwise it gives the first matched row of given criteria.
Syntax:
Model::where('fieldname',$value)->first();
Example:
$user = User::where('email',$email)->first();
//OR
//$user = User::whereEmail($email)->first();
In case you need to find out any specific field, you can use
$user = User::where('email',$email)->first();
$user = User::where('username',$username)->first();
$user = User::where('id',$id)->first();
You can do this too
Before you use this you must declare the DB facade in the controller Simply put this line for that
use Illuminate\Support\Facades\DB;
Now you can get a row using this
$getUserByEmail = DB::table('users')->where('email', $email)->first();
or by this too
$getUserByEmail = DB::select('SELECT * FROM users WHERE email = ?' , ['[email protected]']);
This one returns an array with only one item in it and while the first one returns an object. Keep that in mind.
Hope this helps.