通常我可以用來 CustomerAddress::with(["province", "city", "district"]);包含與響應(yīng)的關(guān)系,但我使用模型作為方法參數(shù),如下所示:public function show(CustomerAddress $address){ return $address;}目前,我可以使用以下方式獲取關(guān)系查詢:public function show(CustomerAddress $address){ $address = CustomerAddress::with(["province", "city", "postalcode", "district"])->where("id", $address->id)->firstOrFail(); return $address;}但我認(rèn)為這會(huì)產(chǎn)生雙重查詢,這對(duì)性能不利。我的另一個(gè)解決方案是不要在參數(shù)中調(diào)用模型,如下所示:public function show($address_id){ $address = CustomerAddress::with(["province", "city", "postalcode", "district"])->where("id", $address_id)->firstOrFail(); return $address;}但由于某種原因,我需要CustomerAddress在方法參數(shù)中使用模型。是否還有其他解決方案可以包含關(guān)系而$address無需再次調(diào)用模型類?
如何在控制器方法中使用與模型參數(shù)的關(guān)系?
動(dòng)漫人物
2023-07-21 18:20:14