现在的业务逻辑是 查询成交订单的客户信息时 会关联这个客户的订单(orders 一对多) ,客户信息(customer一对一的反向),分组信息和订单信息(一对多) ,其中订单模型2个一对多的反向 用户(user)和购买的产品信息(product),
我想在关联查询订单的时候能不能直接带出来订单的关联数据 不用下面的foreach 该如何操作呢
{
$limit = request('limit')?:15;
$data = $customerService->Upper($this->user->team_group_id)->paginate($limit);
$data->load('customer','teamGroup','orders');
if($data->isEmpty())
{
return $this->msg([],"03","并没有搜索到");
}
//return new CustomerServiceCollection($data);
$data = $data->toArray();
foreach ($data['data'] as &$d){
$d['total_price'] = Order::where('customer_id',$d['customer_id'])->sum('price');
$d['first_order'] = Order::where('customer_id',$d['customer_id'])->orderBy('id','asc')->value('collect_date');
foreach ($d['orders'] as &$dd){
$dd['user'] = Order::find($dd['id'])->user;
$dd['product'] = Order::find($dd['id'])->product;
}
}
$ename = request('ename');
//条件筛选
return $this->msg($data);
}