問題描述
在我的一些測試中,我創(chuàng)建了一個用戶模型,并運行了一些需要保存某些屬性的方法.在 Rails 中,我通常會調(diào)用諸如 user.reload
之類的東西,它會重新填充數(shù)據(jù)庫中的屬性.
In some of my tests, I have a user model I have created and I run some methods that need to save certain attributes. In rails, I would typically call something like user.reload
which would repopulate the attributes from the database.
laravel 有沒有辦法做到這一點?我通讀了 api 并找不到它的方法:http://laravel.com/api/4.1/Illuminate/Database/Eloquent/Model.html 任何關于正確"方法的想法?
Is there a way in laravel to do that? I read through the api and couldn't find a method for it: http://laravel.com/api/4.1/Illuminate/Database/Eloquent/Model.html Any ideas on the "right" way to do this?
推薦答案
我也看不到.看起來你必須:
I can't see it either. Looks like you'll have to:
$model = $model->find($model->id);
您也可以自己創(chuàng)建一個:
You can also create one yourself:
public function reload()
{
$instance = new static;
$instance = $instance->newQuery()->find($this->{$this->primaryKey});
$this->attributes = $instance->attributes;
$this->original = $instance->original;
}
剛剛在這里測試了它,它看起來有效,但不確定這能走多遠,Eloquen 是一個相當大的類.
Just tested it here and it looks it works, not sure how far this goes, though, Eloquen is a pretty big class.
這篇關于如何從 Laravel 中的數(shù)據(jù)庫重新加載/刷新模型?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!