Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

laravel select where and where condition

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,

like image 369
spacemonkey Avatar asked Dec 08 '13 15:12

spacemonkey


2 Answers

$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(); 
like image 174
mjhinch Avatar answered Sep 25 '22 01:09

mjhinch


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

like image 40
afarazit Avatar answered Sep 26 '22 01:09

afarazit