第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

如何在控制器方法中使用與模型參數(shù)的關(guān)系?

如何在控制器方法中使用與模型參數(shù)的關(guān)系?

PHP
動(dòng)漫人物 2023-07-21 18:20:14
通常我可以用來 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)用模型類?
查看完整描述

2 回答

?
哈士奇WWW

TA貢獻(xiàn)1799條經(jīng)驗(yàn) 獲得超6個(gè)贊

您已經(jīng)加載了模型,因此只需加載關(guān)系即可。這稱為延遲預(yù)加載。

public?function?show(CustomerAddress?$address){
????return?$address->load("province",?"city",?"postalcode",?"district");
}

希望有幫助:)



查看完整回答
反對(duì) 回復(fù) 2023-07-21
?
呼如林

TA貢獻(xiàn)1798條經(jīng)驗(yàn) 獲得超3個(gè)贊

顯示方法是這樣的

public function show(CustomerAddress $address) { 
return $address::with(["province", "city", "postalcode", "district"])->where("id", $address->id)->firstOrFail();
}

您可以使用 show 方法并傳遞CustomerAddressas 參數(shù)

show(new CustomerAddress())


查看完整回答
反對(duì) 回復(fù) 2023-07-21
  • 2 回答
  • 0 關(guān)注
  • 148 瀏覽

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)