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?
This is what I'm doing now:
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:The issue was discussed here.
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
Update/Disclaimer
This code works, but it will override the regular Eloquent Model
creating
EventAn update to this...
@j-bruni submitted a proposal and Laravel 4.0.x is now supporting using the following:
Which will automatically set your attribute
subject
toA 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:See the Github thread for more info.