Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 653 Vote(s) - 3.45 Average
  • 1
  • 2
  • 3
  • 4
  • 5
get Original Attribute for Eloquent Model Laravel 5.1

#1
I have `Foo` Attribute that use `getFooAttribute` method to format it before display but in some places, I need original attribute for it. So how can I do it?
Reply

#2
If you want to use **mutator** in the majority of the code but sometimes would like to access the original value, you can do it by fetching all attributes using **getAttributes()** method of your model and then fetching the value from there, e.g.:

$originalFoo = $model->getAttributes()['foo'];
Reply

#3
I am using 5.3 and for this I use `$model->getOriginal()['foo']`
Reply

#4
Getting the original value of a particular attribute from v4.2 onwards:

`$originalFoo = $model->getOriginal('foo');`
Reply

#5
for laravel 5 we can also use:

$model->getOriginal('foo')

credits to @bower
Reply

#6
Something that might be worth mentioning as well is that the Laravel model class overwrites the magic methods for `__set` and `__get`, so you can also retrieve the `original` attribute dynamically i.e. `$model->original`.

See

[To see links please register here]

Reply

#7
If you want the raw field straight out of database skipping the mutator then

$mode->getRawOriginal('attribute')
is the way to go. `$model->getOriginal()` will give you the mutator value **not** the raw data you want.

see [

[To see links please register here]

][1]


[1]:

[To see links please register here]

Reply

#8
# Laravel 7 and Symfony 5 respectively


```php
$model->getRawOriginal()
```



> ### The getOriginal Method
> Likelihood Of Impact: Low
>
> 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.

+ *Reference A: [Upgrade Guide: Upgrading To Laravel 7.0 From 6.x](

[To see links please register here]

)*
+ *Reference B: [Laravel 7.x Eloquent API](

[To see links please register here]

)*
Reply

#9
Worked for me

$newProduct = $product[0]->getRawOriginal();
Reply

#10
Actually looks all above are correct, just see if this is in array key [0]
for example:

$originalFoo = $model[0]->getRawOriginal('foo');
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through