I am trying to execute this query:
select
*
FROM
users
left join emails on users.id = emails.user_id
WHERE
users.username = 'firstuser'
and emails.emailType = 'Primary';
This is what I have but it gives me an error:
$user = User::where('username','firstuser')->with('emails')
->where('emailType','Primary')
->get();
The message indicates that the join is not being made:
Column not found: 1054 Unknown column 'emailType' in 'where clause' (SQL: select * from `users` where `username` = firstuser and `emailType` = Primary)
Also, in my User.php model file in have a function emails():
public function emails()
{
return $this->hasMany('Email');
}
In my Email.php model I have a user() function
public function user()
{
return $this->belongsTo('User');
}
I've tried a bunch of combinations but can't get both where conditions to work together. I'd like to do this as one query and not two queries (get userid and then query email with the user_id) Thanks in advance for any and all help!
joinor combo ofwhereHasandwith. What result you expect exactly? Btwwithdoesn't join tables, that's why you get the error.