Laravel 5.5 - 渴望加载 - 删除字段

时间:2018-01-17 17:47:05

标签: laravel laravel-5.5

我正在尝试在我的端点中返回具有角色的用户。但是,在使用Eager Loading时,我不能仅返回角色名称数组。

public function me()
{
     return response()->json(Auth::guard()->user()->with('roles:name')->get());
}

回报是这样的:

[
    {
        "id": 1,
        "username": "userJohn",
        "email": "johhn@gmail.com",
        "avatar": "default.jpg",
        "created_at": "2018-01-17 15:58:16",
        "updated_at": "2018-01-17 15:58:16",
        "roles": [
            {
                "name": "free",
                "pivot": {
                    "user_id": 1,
                    "role_id": 1
                }
            }
        ]
    }
]

但我需要这个:

[
    {
        "id": 1,
        "username": "userJohn",
        "email": "john@gmail.com",
        "avatar": "default.jpg",
        "created_at": "2018-01-17 15:58:16",
        "updated_at": "2018-01-17 15:58:16",
        "roles": [ "free" ]
    }
]

更新:

我试过了:

    return response()->json([
        'user' => $user,
        'roles' => $user->roles->pluck('name'),
    ]);

但是这会导致用户带来角色数组:

{
    "user": {
        "id": 1,
        "username": "johhn",
        "email": "johhn@gmail.com",
        "avatar": "default.jpg",
        "created_at": "2018-01-17 22:17:02",
        "updated_at": "2018-01-17 22:17:02",
        "roles": [
            {
                "id": 1,
                "name": "free",
                "created_at": "2018-01-17 22:10:24",
                "updated_at": "2018-01-17 22:10:24",
                "pivot": {
                    "user_id": 1,
                    "role_id": 1
                }
            }
        ]
    },
    "roles": [
        "free"
    ]
}

2 个答案:

答案 0 :(得分:3)

由于您只获得一个用户,因此可以使用pluck()获得角色:

auth()->user()->roles->pluck('name')

如果您需要精确的结构,则可以使用访问器并将其与$appends属性一起使用。在Laravel 5.5+中,您还可以使用Eloquent Resource class格式化数据。

答案 1 :(得分:1)

您可以使用Laravel 5.5 安装附带的资源类。只需创建一个UserResource类,如下所示。

免责声明:我刚刚修改了文档中提供的示例,您可以查看完整的文档here

<?php

namespace App\Http\Resources;

use Illuminate\Http\Resources\Json\Resource;

class UserResource extends Resource
{
    /**
     * Transform the resource into an array.
     *
     * @param  \Illuminate\Http\Request
     * @return array
     */
    public function toArray($request)
    {
        return [
            'id' => $this->id,
            'name' => $this->name,
            'email' => $this->email,
            'created_at' => $this->created_at,
            'updated_at' => $this->updated_at,
            'roles' => $this->roles()->pluck('name'),
        ];
    }
}

然后在你的控制器中,你可以这样返回一个json:

return new UserResource(User::find(1));

但如果你的版本是5.4或更低,你可以使用变形金刚。请参阅文档here。与laravel的资源类完全相同:

<?php
namespace App\Transformesr;

use App\User;
use League\Fractal;
use Fractal\TransformerAbstract;

class UserTransformer extends TransformerAbstract
{
    public function transform(User $user)
    {
        return [
            'id' => $user->id,
            'name' => $user->name,
            'email' => $user->email,
            'created_at' => $user->created_at,
            'updated_at' => $user->updated_at,
            'roles' => $user->roles()->pluck('name'),
        ];
    }
}

接下来,在您的控制器内:

use League\Fractal\Manager;
use League\Fractal\Resource\Item;
use League\Fractal\Serializer\DataArraySerializer;

// You may need to move this into a separate class
// then simply extend all your Transformers.
$manager = new Manager();
$manager->setSerializer(new DataArraySerializer());

return new Item(User::find(1), new UserTransformer);