Mysql 如何根据父字段检索数据
我有两个表名为Mysql 如何根据父字段检索数据,mysql,laravel,laravel-4,eloquent,Mysql,Laravel,Laravel 4,Eloquent,我有两个表名为user和customer 关系是user.id=customer.user\u id 现在我想在父模型(user)的基础上从customer模型中进行选择,就像这样select c.*from customer as c internal join user as u on c.user\u id=u.id,其中u.status='1' 现在,如何使用laravel eloquent做到这一点,您可以尝试以下方法: $customers = Customer::whereHas(
user
和customer
关系是user.id=customer.user\u id
现在我想在父模型(user
)的基础上从customer
模型中进行选择,就像这样select c.*from customer as c internal join user as u on c.user\u id=u.id,其中u.status='1'
现在,如何使用laravel eloquent做到这一点,您可以尝试以下方法:
$customers = Customer::whereHas('user', function($q) {
$q->where('status', 1);
})->get();
还要确保您在客户
模型中声明了用户
方法:
public function User()
{
return $this->belongsTo('user','user_id','id');
}
非常感谢你。你解决了我的许多问题。我是你的超级粉丝。再次感谢你。