Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Execute code when Eloquent model is retrieved from database

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.

like image 261
severin Avatar asked Dec 08 '22 12:12

severin


2 Answers

You have two valid options:

  • You can subclass \Illuminate\Datebase\Eloquent\Model to add such an event.
  • You can modify your copy of \Illuminate\Datebase\Eloquent\Model to add this (and possibly send an (unsolicited) pull request to Laravel on GitHub). According to Issue 1685, it looks as though they do not want it.

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.

like image 51
Tyler Crompton Avatar answered Dec 11 '22 07:12

Tyler Crompton


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

like image 38
Chris Avatar answered Dec 11 '22 09:12

Chris