重新装载模型在laravel [英] Reloading model in laravel
问题描述
user.reload
,这将重新填充数据库中的属性。 是有一个方法在laravel做到这一点?我读过api,找不到一个方法: http://laravel.com /api/4.1/Illuminate/Database/Eloquent/Model.html 有关正确方式的任何想法?
我也看不到。看起来你必须:
$ model = $ model-> find($ model-> id);
您还可以自己创建一个:
public function reload()
{
$ instance = new static;
$ instance = $ instance-> newQuery() - > find($ this-> {$ this-> primaryKey});
$ this-> attributes = $ instance->属性;
$ this-> original = $ instance-> original;
}
只是在这里测试它,它看起来很有效,不知道这是多远但是,Eloquen是一个很大的班级。
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.
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);
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;
}
Just tested it here and it looks it works, not sure how far this goes, though, Eloquen is a pretty big class.
这篇关于重新装载模型在laravel的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!