Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

what is the difference between $this->attributes and $this->original

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?

enter image description here

like image 784
user391986 Avatar asked Jul 27 '13 18:07

user391986


People also ask

What is the difference between properties and attributes?

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.

How do you use getOriginal in laravel?

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.

What is getOriginal in laravel?

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.


1 Answers

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'];
like image 56
Antonio Carlos Ribeiro Avatar answered Sep 25 '22 03:09

Antonio Carlos Ribeiro