Laravel 8 處理關(guān)聯(lián)關(guān)系

2021-07-06 11:11 更新

因?yàn)橐虞d的 Model 關(guān)聯(lián)關(guān)系也會(huì)被序列化,導(dǎo)致序列化的任務(wù)字符串可能會(huì)變得非常大。為了防止關(guān)系被序列化,您可以在設(shè)置屬性值時(shí)調(diào)用模型上的 withoutRelations 方法。這個(gè)方法會(huì)返回一個(gè)沒有加載關(guān)系的模型實(shí)例:

/**
 * 創(chuàng)建一個(gè)新的 job 實(shí)例
 *
 * @param  \App\Models\Podcast  $podcast
 * @return void
 */
public function __construct(Podcast $podcast)
{
    $this->podcast = $podcast->withoutRelations();
} 
以上內(nèi)容是否對(duì)您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號(hào)
微信公眾號(hào)

編程獅公眾號(hào)