Reputation: 2550
I have this basic query i want to perform but an error keeps coming up. Probably due to my newness to laravel.
here is the code:
$userRecord = $this->where('email', $email)->where('password', $password);
echo "first name: " . $userRecord->email;
I am trying to get the user record matching the credentials where email AND password are a match. This is throwing an error:
Undefined property: Illuminate\Database\Eloquent\Builder::$email
I've checked the email and password being passed to the function, and they are holding values. what is the problem here?
Thanks,
Upvotes: 68
Views: 296525
Reputation: 146
$userRecord = $this->where('email', $email)->where('password', $password);
in the above code , you are just requesting for Eloquent object , not requesting for the data,
$userRecord = $this->where('email', $email)->where('password', $password)->first();
so that, you can get the first data, from the given credentials by default ordering DESC with PK, in case of multiple data with the same credentials. but you have to handle the exception, in case of no matching data. you have one more option to achieve the same.
$userRecord = $this->where('email', $email)->where('password', $password)->firstOrfail();
in the above snippets, in case of no data, it will automatically throw a 404 error.
also, you can have alternative snippets
$filter['email']=$email;
$filter['password']=$password;
$userRecord = $this->where($filter)->first();
That's it
Upvotes: 1
Reputation: 859
Here is shortest way of doing it.
$userRecord = Model::where(['email'=>$email, 'password'=>$password])->first();
Upvotes: 4
Reputation: 4984
You either need to use first()
or get()
to fetch the results :
$userRecord = $this->where('email', $email)->where('password', $password)->first();
You most likely need to use first()
as you want only one result returned.
If the record isn't found null
will be returned. If you are building this query from inside an Eloquent class you could use self
.
for example :
$userRecord = self::where('email', $email)->where('password', $password)->first();
More info here
Upvotes: 15
Reputation: 686
$userRecord = Model::where([['email','=',$email],['password','=', $password]])->first();
or
$userRecord = self::where([['email','=',$email],['password','=', $password]])->first();
I` think this condition is better then 2 where. Its where condition array in array of where conditions;
Upvotes: 11
Reputation: 11119
After reading your previous comments, it's clear that you misunderstood the Hash::make function. Hash::make uses bcrypt hashing. By design, this means that every time you run Hash::make('password'), the result will be different (due to random salting). That's why you can't verify the password by simply checking the hashed password against the hashed input.
The proper way to validate a hash is by using:
Hash::check($passwordToCheck, $hashedPassword);
So, for example, your login function would be implemented like this:
public static function login($email, $password) {
$user = User::whereEmail($email)->first();
if ( !$user ) return null; //check if user exists
if ( Hash::check($password, $user->password) ) {
return $user;
} else return null;
}
And then you'd call it like this:
$user = User::login('email@aol.com', 'password');
if ( !$user ) echo "Invalid credentials.";
else echo "First name: $user->firstName";
I recommend reviewing the Laravel security documentation, as functions already exist in Laravel to perform this type of authorization.
Furthermore, if your custom-made hashing algorithm generates the same hash every time for a given input, it's a security risk. A good one-way hashing algorithm should use random salting.
Upvotes: 6
Reputation: 791
$this->where('email', $email)->where('password', $password)
is returning a Builder object which you could use to append more where filters etc.
To get the result you need:
$userRecord = $this->where('email', $email)->where('password', $password)->first();
Upvotes: 64
Reputation: 9868
The error is coming from $userRecord->email
. You need to use the ->get()
or ->first()
methods when calling from the database otherwise you're only getting the Eloquent\Builder
object rather than an Eloquent\Collection
The ->first()
method is pretty self-explanatory, it will return the first row found. ->get()
returns all the rows found
$userRecord = Model::where('email', '=', $email)->where('password', '=', $password)->get();
echo "First name: " . $userRecord->email;
Upvotes: 6