laravel select where and where condition

LaravelLaravel 4Eloquent

Laravel Problem Overview


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,

Laravel Solutions


Solution 1 - Laravel

$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();

Solution 2 - Laravel

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

Solution 3 - Laravel

$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;

Solution 4 - Laravel

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 protected]', '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.

Solution 5 - Laravel

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;

Solution 6 - Laravel

Here is shortest way of doing it.

$userRecord = Model::where(['email'=>$email, 'password'=>$password])->first();

Solution 7 - Laravel

$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

Solution 8 - Laravel

After rigorous testing, I found out that the source of my problem is Hash::make('password'). Apparently this kept generating a different hash each time. SO I replaced this with my own hashing function (wrote previously in codeigniter) and viola! things worked well.

Thanks again for helping out :) Really appreciate it!

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionspacemonkeyView Question on Stackoverflow
Solution 1 - LaravelmjhinchView Answer on Stackoverflow
Solution 2 - LaravelafarazitView Answer on Stackoverflow
Solution 3 - LaravelMgorunuchView Answer on Stackoverflow
Solution 4 - LaravelTonyArraView Answer on Stackoverflow
Solution 5 - LaravelWaderView Answer on Stackoverflow
Solution 6 - LaravelKaleem ShoukatView Answer on Stackoverflow
Solution 7 - LaravelKishor PantView Answer on Stackoverflow
Solution 8 - LaravelspacemonkeyView Answer on Stackoverflow