dongzhuoxie1244 2018-10-12 01:36
浏览 293

response() - > json()返回“resource”换行 - Laravel 5.6。*

I have a problem regarding the response JSON of my API. I used a resource, since I wanted to limit what data to send back to the client. It was properly giving my intended response before, but when I opened my project again, the response changed.

Here's are parts of my code:

api.php

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

Sample URL: http://localhost:8000/api/admin/adminuserdetails/1

Controller

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

AdminUsers Model

class AdminUsers extends Model
{

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

AdminUserAccessDetails Resource

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
        ];
    }
}

Sample response (before, my intended response)

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

Sample response (now)

{
        {
        "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": []
    }

I didn't change anything, but when I tested it again (not only in this particular route), everything that uses any resource is now enclosed within that resource wrap, and I can't seem to find the reason why.

I tried implementing the same logic with another clean project, and it's working perfectly.

What's causing this and how do I get my intended response back?

Edit 1: I tried to change my return, I removed the "response()->json()" code so my controller would look like:

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

The response of this edit is now a bit closer to my intended output:

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

However I still prefer using the response()->json() so that I can return a proper HTTP response code..

  • 写回答

1条回答 默认 最新

  • duandoucou7200 2018-10-12 03:14
    关注

    I think that the problem is you are sending a Laravel Object as API response. Remember, Laravel Core instance object add some properties/methods to manage/use easily inside your code.

    I may recommend you create a new Class or Associative array to encapsulate specifics properties and later send it.

    For Example:

    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);
    
    }
    

    I hope be useful.

    评论

报告相同问题?

悬赏问题

  • ¥15 安装svn网络有问题怎么办
  • ¥15 Python爬取指定微博话题下的内容,保存为txt
  • ¥15 vue2登录调用后端接口如何实现
  • ¥65 永磁型步进电机PID算法
  • ¥15 sqlite 附加(attach database)加密数据库时,返回26是什么原因呢?
  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥15 latex怎么处理论文引理引用参考文献