Laravel:删除返回结果中的属性
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/45777058/
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
Laravel: Remove an attribute in returned result
提问by Yahya Uddin
I have the following code:
我有以下代码:
$orders = Order::all();
return $orders;
This returns something like this:
这将返回如下内容:
[
{
"id": 123,
"qr_code": "foo.png",
"qr_code_url": "http://example.com/foo.png"
},
{
"id": 112,
"qr_code": "bar.png",
"qr_code_url": "http://example.com/var.png"
}
]
Note that qr_code_url
is an appended attribute, and not an attribute stored in the database.
请注意,这qr_code_url
是一个附加属性,而不是存储在数据库中的属性。
I want to return this collection back to the user without the attribute: qr_code
, in this case. So like this:
qr_code
在这种情况下,我想将此集合返回给没有属性的用户:。所以像这样:
[
{
"id": 123,
"qr_code_url": "http://example.com/foo.png"
},
{
"id": 112,
"qr_code_url": "http://example.com/var.png"
}
]
Looking at the collection functions, i can't seem to find an easy way to do this: https://laravel.com/docs/5.4/collections
查看收集功能,我似乎找不到一种简单的方法来做到这一点:https: //laravel.com/docs/5.4/collections
The only functions I have found close to what I want is: except
and forget
, but they seem to just work on a 1 dimension array. Not a collection result returned by a model.
我发现的唯一接近我想要的函数是: except
and forget
,但它们似乎只适用于一维数组。不是模型返回的集合结果。
How can I solve my problem?
我该如何解决我的问题?
回答by Elias Soares
You can set your attribute as hidden on the model class (see Hidding Attributes From Json)
您可以在模型类上将您的属性设置为隐藏(请参阅从 Json 隐藏属性)
/**
* The attributes that should be hidden for serialization.
*
* @var array
*/
protected $hidden = ['qr_code'];
The attribute will still be loaded, but it won't be shown in your collections.
该属性仍将被加载,但不会显示在您的集合中。
If you don't want to make it permanent, you can use the makeHidden()
eloquent method as described on the docs:
如果您不想使其永久化,您可以使用makeHidden()
文档中描述的eloquent 方法:
Temporarily Modifying Attribute Visibility
If you would like to make some typically hidden attributes visible on a given model instance, you may use the makeVisible method. The makeVisible method returns the model instance for convenient method chaining:
return $user->makeVisible('attribute')->toArray();
Likewise, if you would like to make some typically visible attributes hidden on a given model instance, you may use the makeHidden method.
return $user->makeHidden('attribute')->toArray();
临时修改属性可见性
如果你想让一些典型的隐藏属性在给定的模型实例上可见,你可以使用 makeVisible 方法。makeVisible 方法返回模型实例以方便方法链接:
return $user->makeVisible('attribute')->toArray();
同样,如果您想在给定的模型实例上隐藏一些通常可见的属性,您可以使用 makeHidden 方法。
return $user->makeHidden('attribute')->toArray();
回答by kmuenkel
$eloquentCollection->transform(function (Model $result) use ($forgetThisKey) {
$attributes = $result->getAttributes();
unset($attributes[$forgetThisKey]);
$result->setRawAttributes($attributes, true);
return $result;
});