Overload a Laravel Model’s __construct and save Methods

Here are a couple of quick snippets if you need the ability to overload a Model’s constructor or save methods.

We’ll use the following example model



use Illuminate\Auth\Reminders\RemindableTrait;
use Illuminate\Auth\Reminders\RemindableInterface;

class User extends Eloquent implements RemindableInterface {

	use RemindableTrait;

	/**
	 * The database table used by the model.
	 *
	 * @var string
	 */
	protected $table = 'users';

	/**
	 * The attributes excluded from the model's JSON form.
	 *
	 * @var array
	 */
	protected $hidden = array();
}

Let’s say we wanted to be able to set the name of the User when instantiating the model. Insert the following into the Model.



use Illuminate\Auth\Reminders\RemindableTrait;
use Illuminate\Auth\Reminders\RemindableInterface;

class User extends Eloquent implements RemindableInterface {

	use RemindableTrait;

	/**
	 * The database table used by the model.
	 *
	 * @var string
	 */
	protected $table = 'users';

	/**
	 * The attributes excluded from the model's JSON form.
	 *
	 * @var array
	 */
	protected $hidden = array();

        /**
	 * Overload model constructor.
	 *
	 * $name string Sets the User's name (Optional)
	 */
        public function __construct ($name = null, $attributes = array())
	{
            parent::__construct($attributes); // Calls Default Constructor
            
            $this->name = is_null($name) ? $name : null;

            // Do great things...
        }
}

You can pass unlimited parameters into the method, as long as the last parameter you pass is$attributs = array(). You also need to call the parent::__construct(). This can be done before or after your code execution.

The same logic applies with the save method. Let’s say we wanted to verify that the User’s name was equal to a given input.



use Illuminate\Auth\Reminders\RemindableTrait;
use Illuminate\Auth\Reminders\RemindableInterface;

class User extends Eloquent implements RemindableInterface {

	use RemindableTrait;

	/**
	 * The database table used by the model.
	 *
	 * @var string
	 */
	protected $table = 'users';

	/**
	 * The attributes excluded from the model's JSON form.
	 *
	 * @var array
	 */
	protected $hidden = array();

        /**
	 * Overload model constructor.
	 *
	 * $name string Sets the User's name (Optional)
	 */
        public function __construct ($name = null, $attributes = array())
	{
            parent::__construct($attributes); // Calls Default Constructor
            
            $this->name = is_null($name) ? $name : null;

            // Do great things...
        }

        /**
	 * Overload model save.
	 *
	 * $name_equals string Assert User's name (Optional)
	 */
        public function __save ($name_equals = null, array $options = array())
	{   
            if (!is_null($name_equals))
            {
                if ($name_equals === $this->name) { return false; }
            }

            // Do great things...

            parent::save($options); // Calls Default Save
        }
}

The code checks if we are passing in a name to verify against. If a name is passed, verify that it equals the name of the User, if it does not, return false. If no name is passed, then continue to save as normal by calling parent::save().

Here is the complete code, sans highlighting.



use Illuminate\Auth\Reminders\RemindableTrait;
use Illuminate\Auth\Reminders\RemindableInterface;

class User extends Eloquent implements RemindableInterface {

	use RemindableTrait;

	/**
	 * The database table used by the model.
	 *
	 * @var string
	 */
	protected $table = 'users';

	/**
	 * The attributes excluded from the model's JSON form.
	 *
	 * @var array
	 */
	protected $hidden = array();

        /**
	 * Overload model constructor.
	 *
	 * $name string Sets the User's name (Optional)
	 */
        public function __construct ($name = null, $attributes = array())
	{
            parent::__construct($attributes); // Calls Default Constructor
            
            $this->name = is_null($name) ? $name : null;

            // Do great things...
        }

        /**
	 * Overload model save.
	 *
	 * $name_equals string Assert User's name (Optional)
	 */
        public function __save ($name_equals = null, array $options = array())
	{   
            if (!is_null($name_equals))
            {
                if ($name_equals === $this->name) { return false; }
            }

            // Do great things...

            parent::save($options); // Calls Default Save
        }
}