为什么我的 Laravel Eloquent 访问器没有出现在响应中?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/47872233/
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-09-14 17:07:35  来源:igfitidea点击:

Why is my Laravel Eloquent accessor not showing up in the response?

laravellaravel-5eloquentlaravel-5.5accessor

提问by online Thomas

I have a Model Reviewthat has a unix timestamp as 1 of it's attributes (table columns).

我有一个模型Review,它有一个 unix 时间戳作为它的属性之一(表列)。

I use 2 accessorsinside this model:

我在这个模型中使用了 2 个访问器

public function getReviewDateAttribute($value)
{
    return strftime('%A %e %B %Y', $value);
}

public function getReviewDateIsoAttribute()
{
    return Carbon::createFromTimestamp($this->review_date)->toDateTimeString();
}

getReviewDateAttributeworks as expected and shows up in the collection of models when I write a query.

getReviewDateAttribute按预期工作并在我编写查询时显示在模型集合中。

However getReviewDateIsoAttributedoes not. What could be the reason for this?

然而getReviewDateIsoAttribute没有。这可能是什么原因?

A subquestion: If I use the same attribute in both functions, how can I use the original format as input value?

一个子问题:如果我在两个函数中使用相同的属性,如何使用原始格式作为输入值?

enter image description here

enter image description here

回答by lagbox

You should be adding it to the $appendsarray. It isn't spinning through all available methods looking for getXXXXXAttribute. The other one is used because it is an accessor for an actual attribute, this one is not an actual attribute.

您应该将其添加到$appends数组中。它不会遍历所有可用的方法来寻找getXXXXXAttribute. 使用另一个是因为它是实际属性的访问器,这个不是实际属性。

class YourModel ....
{
     protected $appends = ['review_date_iso'];
     ...
}

Laravel 5.5 Docs - Eloquent - Serialization - Appending Values to JSON

Laravel 5.5 文档 - Eloquent - 序列化 - 将值附加到 JSON

回答by kofoworola

This is probably because ReviewDateIsois not an actual column and therefore will not show up in the model collections...you can access it directly by calling the method directly

这可能是因为ReviewDateIso它不是实际的列,因此不会出现在模型集合中……您可以通过直接调用该方法来直接访问它

$model->getReviewDateIsoAttribute()

According to the docs accessors a ways of changing the value of a column before it is returned to the method that queried it.

根据文档访问器,在返回到查询它的方法之前更改列值的方法。

If you want it to show up when you call the collection as Json append it to the output with

如果您希望在将集合作为 Json 调用时显示它,请将其附加到输出中

protected $appends = ['name_of_attribute'];

回答by Marcin Nabia?ek

In fact it depends on what you are trying to achieve. If you are going to display anything in Blade for example, you can use whenever you want $object->review_date_isoto get this attribute value.

实际上,这取决于您要实现的目标。例如,如果您要在 Blade 中显示任何内容,则可以在想要$object->review_date_iso获取此属性值的任何时候使用。

However if you are returning Json responses (for example API), you need to tell Eloquent model to append extra fields to your model when transforming to JSON format.

但是,如果您要返回 Json 响应(例如 API),则需要告诉 Eloquent 模型在转换为 JSON 格式时将额外的字段附加到您的模型中。

You probably have review_dateattribute in your model (this is the column in database I suppose) but you don't have review_date_isoin your table in database, so in your model you need to use:

review_date的模型中可能有属性(我想这是数据库中的列),但review_date_iso数据库中的表中没有,因此在您的模型中,您需要使用:

protected $appends = ['review_date_iso'];

but you don't have to include here review_datebecause it's already in your table in database and your accessor will be automatically fired.

但您不必在此处包含,review_date因为它已经在您的数据库表中,并且您的访问器将被自动触发。

回答by Vineeth Vijayan

I tried queries in tinker and with dd method. So I got confused since I cant view my new accessor in the attribute list.

我尝试使用 tinker 和 dd 方法进行查询。所以我很困惑,因为我无法在属性列表中查看我的新访问器。

I thought I missed something. Then later I noticed appended attributes are shown separately when we dd the query result than the usual attribute JSON.

我以为我错过了什么。后来我注意到当我们 dd 查询结果而不是通常的属性 JSON 时,附加的属性是单独显示的。

I lost time thinking my accessor is not working. Its just I`m not checking in the right way or right place. So I``m attaching the following image, if anyone gets stuck on accessors like me, this might help them.

我浪费了时间以为我的存取器不工作。只是我没有以正确的方式或正确的地方检查。所以我附上了下面的图片,如果有人像我一样卡在访问器上,这可能会对他们有所帮助。

enter image description here

enter image description here