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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 16:31:35  来源:igfitidea点击:

Laravel: Remove an attribute in returned result

laravellaravel-5laravel-5.1

提问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_urlis 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: exceptand forget, but they seem to just work on a 1 dimension array. Not a collection result returned by a model.

我发现的唯一接近我想要的函数是: exceptand 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;
});

回答by Mirceac21

When you build api the recommended way of controlling output data is to use fractaltransformers.

当您构建 api 时,推荐的控制输出数据的方法是使用分形转换器。

If it's to much and you want to keep it simple you can use laravel pluckmethod on collections.

如果它太多并且你想保持简单,你可以pluck在集合上使用 laravel方法。