php 获取 Eloquent 模型 Laravel 5.1 的原始属性

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/34181119/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 23:48:33  来源:igfitidea点击:

get Original Attribute for Eloquent Model Laravel 5.1

phplaravellaravel-5laravel-5.1

提问by Mohamed Gamal

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

我有使用 getFooAttribute 方法在显示前对其进行格式化的 Foo 属性,但在某些地方,我需要它的原始属性..所以我该怎么做

回答by Bower

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

从 v4.2 开始获取特定属性的原始值:

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

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

回答by jedrzej.kurylo

If you want to use mutatorin 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.:

如果您想在大部分代码中使用mutator但有时想要访问原始值,您可以通过使用模型的getAttributes()方法获取所有属性然后从那里获取值来实现,例如:

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

回答by Dewan159

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

我正在使用 5.3,为此我使用 $model->getOriginal()['foo']

回答by Ammad Khalid

for laravel 5 we can also use:

对于 Laravel 5,我们还可以使用:

$model->getOriginal('foo')

credits to @bower

归功于@bower

回答by Ali A. Dhillon

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

如果您希望直接从数据库中获取原始字段跳过 mutator,那么

$mode->getRawOriginal('attribute')

is the way to go. $model->getOriginal()will give you the mutator value notthe raw data you want.

是要走的路。$model->getOriginal()会给你 mutator 值而不是你想要的原始数据。

see https://laravel.com/api/7.x/Illuminate/Database/Eloquent/Concerns/HasAttributes.html#method_getRawOriginal

https://laravel.com/api/7.x/Illuminate/Database/Eloquent/Concerns/HasAttributes.html#method_getRawOriginal

回答by Philipp

Laravel 7 and Symfony 5 respectively

Laravel 7 和 Symfony 5 分别

$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 getRawOriginalmethod instead.

getOriginal 方法

影响可能性:低

$model->getOriginal()方法现在将尊重模型上定义的任何强制转换和修改器。以前,此方法返回未转换的原始属性。如果您想继续检索原始的、未转换的值,您可以改用该getRawOriginal方法。

回答by Angel Santiago Jaime Zavala

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

值得一提的是,Laravel 模型类覆盖了__setand的魔法方法__get,因此您还可以original动态检索属性,即$model->original

See https://github.com/laravel/framework/blob/5.1/src/Illuminate/Database/Eloquent/Model.php

https://github.com/laravel/framework/blob/5.1/src/Illuminate/Database/Eloquent/Model.php