簡體   English   中英

Laravel雄辯的模型解除綁定

[英]Laravel Eloquent Model Unbindings

我最近開始使用Laravel,我想知道是否有一種方法可以解除特定調用中的模型關系綁定。 例如,如果我的一個模型具有hasOne綁定,則我有一個特定的調用,我不想檢索該關系以消除額外的數據庫調用。 我知道這在CakePHP中是可能的,但尚未在Laravel中找到一種方法。

謝謝

如果指定,則僅渴望加載Laravel中的關系。 如果您不希望加載該關系,則在您第一次嘗試訪問該關系時,它將被延遲加載,使其看起來好像已被加載。

用戶類別:

class User extends Eloquent
{
    public function userTransactions()
    {
        return $this->hasMany('UserTransaction');
    }
}

渴望加載:

// runs one query to get the user, and a second query to get the user
// transactions for the user
$user = User::with('userTransactions')->find(1);

延遲加載:

// runs one query to get the user
$user = User::find(1);

// first time the userTransactions property is accessed, it runs the query
// to get the user transactions for the user.
print_r($user->userTransactions->lists('id'));

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM