我有一个Order与 和 具有多态客户关系Parent的模型School。我现在的目标是查询订单并生成包含客户name和的报告email。name和字段都email存在于Parent和中School。
Order有:
public function customer()
{
return $this->morphTo();
}
Parent两者School都有:
public function orders()
{
return $this->morphMany(Order::class, 'customer');
}
我目前的查询是:
$result = Order::query()
->with('customer')
->select(
'orders.id',
'amount'
)->get();
dd($result->first()->toArray());
结果是:
array:6 [▼
"id" => 1
"amount" => "42.00"
"customer" => null
]
我认为可以在选定字段列表中添加customer.name和customer.email,但 Eloquent 抱怨customer.name找不到客户字段,这有意义吗?
如何获取相关模型中的数据?