Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/laravel/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Mysql 如何根据父字段检索数据_Mysql_Laravel_Laravel 4_Eloquent - Fatal编程技术网

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');
}

非常感谢你。你解决了我的许多问题。我是你的超级粉丝。再次感谢你。