通常我可以用来 CustomerAddress::with(["province", "city", "district"]);包含与响应的关系,但我使用模型作为方法参数,如下所示:
public function show(CustomerAddress $address)
{
return $address;
}
目前,我可以使用以下方式获取关系查询:
public function show(CustomerAddress $address)
{
$address = CustomerAddress::with(["province", "city", "postalcode", "district"])->where("id", $address->id)->firstOrFail();
return $address;
}
但我认为这会产生双重查询,这对性能不利。我的另一个解决方案是不要在参数中调用模型,如下所示:
public function show($address_id)
{
$address = CustomerAddress::with(["province", "city", "postalcode", "district"])->where("id", $address_id)->firstOrFail();
return $address;
}
但由于某种原因,我需要CustomerAddress在方法参数中使用模型。是否还有其他解决方案可以包含关系而$address无需再次调用模型类?
哈士奇WWW
呼如林