I put a breakpoint inside a function in my model in Laravel 4.
I see on $this
there is both attributes and original.
Both seem to contain the same data, i.e. a key/value pair representing fields in the table.
What's the difference?
I need a function in my model that will return all the fields as an associative array, which one do I use?
An attribute is a quality or character ascribed to or considered to belong to, or be inherent in, a person or thing. A property is a quality or characteristic belonging to a person or thing, with its original use implying ownership, and also either being essential or special.
Quick example: 1) assume $model gets loaded from the DB and the value of $model->getOriginal('foo') is "foo". 2) Change value of the attribute: $model->foo = "bar"; 3) $model->getOriginal('foo') will return "foo" instead of "bar". @jedrzej. kurylo That is why you should use the setter $model->setFoo("bar") instead.
The $model->getOriginal() method will now respect any casts and mutators defined on the model. Previously, this method returned the uncast, raw attributes. If you would like to continue retrieving the raw, uncast values, you may use the getRawOriginal method instead.
They have the same data until you change any of your attributes:
$user->name = "user391986";
Then you'll have
$user->attributes['name'] != $user->original['name'];
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With