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 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 关于大棚监测的pcb板设计
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用