Dingo Api 如何返回数据表关联结果

@JellyBool,本人在使用 dingo/api 组件时,不知道如何返回数据表的关联结果。特来请教。
先上代码。

    /**
     * 创建一个新用户,同时设置角色与所属部门
     * @Post("/")
     * @Versions({"v1"})
     * @Transaction({
     *      @Request({"name": "admin", "email": "[email protected]","phone":"18679152251","role_id":"2",
     *     "department_id":"1"},
     * headers={"Authorization": "Bearer  token_value"}),
     *      @Response(200, body={"data": {"id": 10, "username": "admin"}),
     *      @Response(500, body={"message": "xxx","code":"23000"})
     * })
     * @Parameters ({
     *      @Parameter("name", description="用户名",required=true),
     *      @Parameter("phone", description="用户手机.",required=true),
     *      @Parameter("email", description="用户邮箱.",required=true),
     *      @Parameter("role_id", description="角色 id.",required=true),
     *      @Parameter("department_id", description="部门 id.",required=true),
     * })
     * @param AddUserRequest $request 表单提交请求
     * @return \Dingo\Api\Http\Response|void
     */
    public function store(AddUserRequest $request)
    {
        $input = [
            'email' => $request->get('email'),
            'name' => $request->get('name'),
            'password' => password_hash(self::DEFAULT_PASSWORD, PASSWORD_DEFAULT),
            'role_id' => $request->get('role_id'),
            'department_id' => $request->get('department_id'),
            'phone' => $request->get('phone'),
        ];

        try{
            DB::transaction(function () use ($input){
                $user = User::create($input);

                $role = Role::findOrFail($input['role_id']);
                $user->attachRole($role);

                $department = Department::findOrFail($input['department_id']);
                $user->departments()->save($department);
            });

            $user = User::where('email','=',$input['email'])->with('departments')->with('roles')->get();

            return $this->response->item($user, new UserTransformer());

        }catch (Exception $e){
            return $this->response->errorInternal('用户创建失败');
        }

    }

其中,UserTransformer 代码如下:

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

在 UsersController 里的结果返回,我自己觉得使用如下代码是不对的。因为查询到的结果是3个表的关联查询后的结合。如何使用 Transformer 处理 这种情况。

return $this->response->item($user, new UserTransformer());
JellyBool
修改的评论也不能少于六个字哦!
Addison 回复 JellyBool
修改的评论也不能少于六个字哦!