【问题标题】:response()->json() returns with "resource" wrap - Laravel 5.6.*response()->json() 以“资源”包装返回 - Laravel 5.6.*
【发布时间】:2019-03-17 04:25:12
【问题描述】:

我的 API 的响应 JSON 有问题。我使用了一个资源,因为我想限制将哪些数据发送回客户端。之前它正确地给出了我想要的响应,但是当我再次打开我的项目时,响应发生了变化。

这是我的部分代码:

api.php

Route::get('admin/adminuserdetails/{adminUser}', 'AdminsController@AdminUserDetails');

示例网址: http://localhost:8000/api/admin/adminuserdetails/1

控制器

public function AdminUserDetails(AdminUsers $adminUser){
    return response()->json(new AdminUserAccessDetails($adminUser), 200);
}

AdminUsers 模型

class AdminUsers extends Model
{

//
   protected $table = 'AdminUsers';
   protected $primaryKey = 'AdminUserId';
   protected $guarded = [];
}

AdminUserAccessDetails 资源

class AdminUserAccessDetails extends JsonResource
{
    public function toArray($request)
    {
        //return parent::toArray($request);
        return [
        'adminUserId' => $this->AdminUserId,
        'adminFirstName' => $this->AdminFirstName,
        'adminLastName' => $this->AdminLastName,
        'modulesAllowed' => $this->ModulesAllowed,
        'actionsAllowed' => $this->ActionsAllowed
        ];
    }
}

示例响应(之前是我的预期响应)

{
    "adminUserId": 1,
    "adminFirstName": "asdfasdf",
    "adminLastName": "asdfsadf",
    "modulesAllowed": "",
    "actionsAllowed": ""
}

示例响应(现在)

{
        {
        "resource": {
            "adminUserId": 1,
            "adminFirstName": "asdfasdf",
            "adminLastName": "asdfsadf",
            "adminEmail": "asdfsadf@fsafsa.com",
            "adminPassword": "l6wfDtAaYAp6aM04TU++9A==",
            "authToken": "68bbc9fc7eb08c9f6d96f6b63d30f056",
            "fCMToken": null,
            "profileImage": "https://www.gravatar.com/avatar/5d0d65256e8c2b15a8d00e8b208565f1?d=identicon&s=512",
            "userTypeId": "0",
            "status": "A",
            "createDate": "2018-06-26 16:01:43.947",
            "updateDate": "2018-06-26 16:01:44.143",
            "modulesAllowed": "",
            "actionsAllowed": ""
        },
        "with": [],
        "additional": []
    }

我没有改变任何东西,但是当我再次测试它时(不仅在这个特定的路线中),使用任何资源的所有内容现在都包含在该资源包装中,我似乎无法找到原因。

我尝试用另一个干净的项目实现相同的逻辑,它运行良好。

这是什么原因造成的,我该如何获得预期的回复?

编辑 1: 我试图改变我的回报,我删除了“response()->json()”代码,所以我的控制器看起来像:

public function AdminUserDetails(AdminUsers $adminUser){
        //return response()->json(new AdminUserAccessDetails($adminUser), 200);
        return new AdminUserAccessDetails($adminUser);
    }

此编辑的响应现在更接近我的预期输出:

{
    "data": {
        "adminUserId": 1,
        "adminFirstName": "asdfasdf",
        "adminLastName": "asdfsadf",
        "modulesAllowed": "",
        "actionsAllowed": ""
    }
}

但是我仍然更喜欢使用 response()->json() 以便我可以返回正确的 HTTP 响应代码..

【问题讨论】:

    标签: php json laravel


    【解决方案1】:

    我认为问题在于您将 Laravel 对象作为 API 响应发送。请记住,Laravel Core 实例对象添加了一些属性/方法以便在您的代码中轻松管理/使用。

    我可能会建议您创建一个新的类或关联数组来封装特定属性,然后再发送。

    例如:

    public function AdminUserDetails(AdminUsers $adminUser){
    
        // Example with Object
        $Object = new YourClass(); // you can define with yours wished properties
        $Object->PropertyA = $adminUser->Property1;
        $Object->PropertyB = $adminUser->Property2;
        // ...
    
        return response()->json($Object, 200);
    
        // Example with Associative Array
        $AssociateArray = array(
            "PropertyA" => $adminUser->Property1,
            "PropertyB" => $adminUser->Property2,
            // ...
        );
    
        return response()->json($AssociateArray, 200);
    
    }
    

    希望对你有用。

    【讨论】:

    • Hmmmm.. 这可能是一种变通解决方案,但一个问题可能是:如果我需要几乎所有这些,除了一个字段用于特定返回?我可能不得不再次列出除一个之外的所有字段,这会使代码变得更长且冗长......
    猜你喜欢
    • 2018-05-31
    • 1970-01-01
    • 1970-01-01
    • 2014-03-09
    • 2018-04-06
    • 2018-12-10
    • 2015-08-08
    • 2015-09-15
    • 1970-01-01
    相关资源
    最近更新 更多