Reputation: 137
I got two tables. I want to get all users and their details who are active/banned.
What is laravel eloquent equivalent to this query.
'select * from client_details where user_id IN (select user_id from client_logins where is_active='.$active.')'
Table: client_logins Model clientLogin
+---------+---------+--------------+-------------+-----------+
| id | user_id | username | password | is_active |
+---------+---------+--------------+-------------+-----------+
Table: client_details Model clientDetail
+---------+------+-------+------------------+
| user_id | name | email | mobile | address|
+---------+------+-------+------------------+
Upvotes: 1
Views: 528
Reputation: 64476
Using query builder you could write it as
$clients=DB::table('client_details as c')
->select('c.*')
->join('client_logins as l', 'c.user_id', '=', 'l.user_id')
->where('l.is_active', '=', $active)
->get()
;
Or if have defined has
relation for ClientDetails
model you can use whereHas
filter
class ClientDetails extends Model
{
public function client_logins()
{
return $this->hasMany('App\ClientLogins', 'user_id', 'user_id');
}
}
$clients = ClientDetails::whereHas('client_logins', function ($query) use ($active) {
$query->where('is_active', '=', $active);
})->get();
Upvotes: 1