Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set a default attribute value for a Laravel / Eloquent model?

If I try declaring a property, like this:

public $quantity = 9;

...it doesn't work, because it is not considered an "attribute", but merely a property of the model class. Not only this, but also I am blocking access to the actually real and existent "quantity" attribute.

What should I do, then?

like image 714
J. Bruni Avatar asked Sep 11 '13 17:09

J. Bruni


People also ask

How do I change the default value in Laravel?

4) Laravel Migration Default Value with Update: $table->boolean('displayed')->default(0)->change(); you can use as you need.

What is fillable attribute in a Laravel model?

In eloquent ORM, $fillable attribute is an array containing all those fields of table which can be filled using mass-assignment. Mass assignment refers to sending an array to the model to directly create a new record in Database.

What is attribute casting in Laravel?

Attribute casting means, changing the value of an attribute to a particular data type like boolean, integer, strings or array. The $casts property should be an array where the key is the name of the attribute and value is the type of attribute being cast. The supported cast types for Laravel Eloquent models are: object.

Is eloquent an ORM?

Eloquent is an object relational mapper (ORM) that is included by default within the Laravel framework. An ORM is software that facilitates handling database records by representing data as objects, working as a layer of abstraction on top of the database engine used to store an application's data.


3 Answers

An update to this...

@j-bruni submitted a proposal and Laravel 4.0.x is now supporting using the following:

protected $attributes = array(
  'subject' => 'A Post'
);

Which will automatically set your attribute subject to A Post when you construct. You do not need to use the custom constructor he has mentioned in his answer.

However, if you do end up using the constructor like he has (which I needed to do in order to use Carbon::now()) be careful that $this->setRawAttributes() will override whatever you have set using the $attributes array above. For example:

protected $attributes = array(
  'subject' => 'A Post'
);

public function __construct(array $attributes = array())
{
    $this->setRawAttributes(array(
      'end_date' => Carbon::now()->addDays(10)
    ), true);
    parent::__construct($attributes);
}

// Values after calling `new ModelName`

$model->subject; // null
$model->end_date; // Carbon date object

// To fix, be sure to `array_merge` previous values
public function __construct(array $attributes = array())
{
    $this->setRawAttributes(array_merge($this->attributes, array(
      'end_date' => Carbon::now()->addDays(10)
    )), true);
    parent::__construct($attributes);
}

See the Github thread for more info.

like image 93
cmfolio Avatar answered Oct 01 '22 13:10

cmfolio


This is what I'm doing now:

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...


UPDATE:

As pointed by cmfolio, the actual ANSWER is quite simple:

Just override the $attributes property! Like this:

protected $attributes = array(
   'quantity' => 9,
);

The issue was discussed here.

like image 45
J. Bruni Avatar answered Oct 01 '22 13:10

J. Bruni


I know this is really old, but I just had this issue and was able to resolve this using this site.

Add this code to your model

protected static function boot()
{
   parent::boot();

   static::creating(function ($model) {
        $model->user_id = auth()->id();
    });
}

Update/Disclaimer

This code works, but it will override the regular Eloquent Model creating Event

like image 41
pbgneff Avatar answered Oct 01 '22 15:10

pbgneff