Execute code when Eloquent model is retrieved from

2019-03-22 00:53发布

I have an Eloquent model. Whenever it is retrieved from the database I would like to check whether a condition is fulfilled and set a model attribute if this is the case.

EDIT: I initially thought that the restoring event would be the right place to put the relevant logic, but as Tyler Crompton points out below, restoring is fired before a soft-deleted record is restored.

2条回答
够拽才男人
2楼-- · 2019-03-22 01:13

You could do this on the way in, or the way out. It seems like you wanted it stored in the database, so you could use mutators.

class Foo extends Eloquent {

    public function setBAttribute($value)
    {
        if ($this->attributes['a'] == $this->attributes['b']) {
            $this->attributes['b'] = 1;
        }
    }    
}

When ever B is set, it will check against A, and store 1 in B.

Side note: Note the B between set and attribute

查看更多
ゆ 、 Hurt°
3楼-- · 2019-03-22 01:29

You have two valid options:

If I were you, I'd go with the first option and this is how I'd do it:

<?php namespace \Illuminate\Database\Eloquent;

abstract class LoadingModel extends Model {

    /**
     * Register a loaded model event with the dispatcher.
     *
     * @param  \Closure|string  $callback
     * @return void
     */
    public static function loaded($callback)
    {
        static::registerModelEvent('loaded', $callback);
    }

    /**
     * Get the observable event names.
     *
     * @return array
     */
    public function getObservableEvents()
    {
        return array_merge(parent::getObservableEvents(), array('loaded'));
    }

    /**
     * Create a new model instance that is existing.
     *
     * @param  array  $attributes
     * @return \Illuminate\Database\Eloquent\Model|static
     */
    public function newFromBuilder($attributes = array())
    {
        $instance = parent::newFromBuilder($attributes);

        $instance->fireModelEvent('loaded', false);

        return $instance;
    }

}

Just make sure the models in question subclass from LoadingModule. I have confirmed this to work as I found a great use case for it. Older versions of PHP returned MySQL values as strings. Normally, PHP will silently cast these to their respective numeric types in numeric operations. However, converting to JSON is not considered a numeric operation. The JSON values are represented as strings. This can cause problems for clients of my API. So I added a loaded event to my models to convert values to the correct type.

查看更多
登录 后发表回答