Nur Uddin
Nur Uddin

Reputation: 2958

How to retrieve data on the basis of parent field

I have two table named user and customer.

The relation is user.id = customer.user_id.

Now I want to select from customer model on the basis of Parent model (user), like this select c.* from customer as c inner join user as u ON c.user_id = u.id WHERE u.status = '1'

Now how to do that with laravel eloquent

Upvotes: 0

Views: 46

Answers (1)

The Alpha
The Alpha

Reputation: 146239

You may try this:

$customers = Customer::whereHas('user', function($q) {
    $q->where('status', 1);
})->get();

Also make sure you have user method declared in your Customer Model:

public function User()
{
    return $this->belongsTo('user','user_id','id');
}

Upvotes: 1

Related Questions