如何在laravel关系中使用where?
我与如何在laravel关系中使用where?,laravel,Laravel,我与订单和客户有关系。 客户表中的一行: 客户识别码 客户名称 客户状态 客户城市 1. 阿明 亚兹德 亚兹德 您将get()放错了位置更改此设置 $reportItem = Order::whereHas("customer", function ($query) use ($state) { $query->where('customer_state', $state)->get(); }); 到 了解更多有关我从给定输入中了解到的内容,此处您没有使
订单
和客户
有关系
。
客户表中的一行: 客户识别码 客户名称 客户状态 客户城市 1. 阿明 亚兹德 亚兹德 您将
get()
放错了位置更改此设置
$reportItem = Order::whereHas("customer", function ($query) use ($state) {
$query->where('customer_state', $state)->get();
});
到
了解更多有关我从给定输入中了解到的内容,此处您没有使用正确的关系:
belongtomany
关系用于使用分配表的多对多关系。但实际上您使用的是一对多关系=>一个客户可以有多个订单。
在这种情况下,您应该在订单模型上使用belongsTo
关系,或/或在客户模型上使用具有多个关系:
//Order.php
公共功能客户(){
返回$this->belongsTo(客户::类)
}
//Customer.php
公共职能令(){
返回$this->hasMany(订单::类)
}
“它不起作用。”-具体来说,什么东西不起作用?您需要解释您的问题,包括您期望的结果、实际发生的情况以及任何错误。@Timleis“它不起作用。”表示其中('customer\u state',$state)
不起作用;工作不正常…感谢您的回复,但我以前做过,问题没有解决。dd($reportItem)
它显示了什么?这显示了所有行。。。
$reportItem = Order::whereHas("customer", function ($query) use ($state) {
$query->where('customer_state', $state);
})->get();