How to reload/refresh model from database in Laravel?

前端 未结 5 1143
既然无缘
既然无缘 2020-12-14 05:39

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 us

5条回答
  •  天命终不由人
    2020-12-14 06:09

    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.

提交回复
热议问题