Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • How to set a default attribute value for a Laravel / Eloquent model?

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 525
    Comment on it

    Laravel 4.x provide n number of services to solve basic issues. By using Laravel 4.x we will easily set a default attribute for a Laravel/Eloquent model.

    To achieve this we have to define value like below example:

    Example:

    protected $defaults = array(
       'quantity' =9,
    );
    
    public function __construct(array $attributes = array())
    {
        $this->setRawAttributes($this->defaults, true);
        parent::__construct($attributes);
    }
    

    I will suggest this as a PR so we don't need to declare this constructor at every Model, and can easily apply by simply declaring the $defaults array in our models...

    By this way we can set default default attribute value for a laravel.

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: