问一下大家,API资源显示到前端的时候有data[ ]包裹,怎么把这个data去了?

<?php

namespace App\Http\Resources;

use Illuminate\Http\Resources\Json\JsonResource;

class PreHomeDoing extends JsonResource
{
    /**
     * Transform the resource collection into an array.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return array
     */
    public function toArray($request)
    {
        //原始的数据
        $data = parent::toArray($request);
        $data['test'] = 'test';
        //$data['nickname'] = $this->ytuser->nickname;
        //$data['avatar']=$this->ytuser->headimgurl;
        return $data;
    }
}

前端显示:

"data": [
        {
            "doid": 350792,
            "uid": 1117860,
            "message": "正面有什么变化吗?一个是12月份 ,一个是现在。",
            },
            "pre_home_doing_image": {
                "id": 473257,
                "doid": 350792,
                "url": "/B599765A-8B32-44B0-967A-818366357038.jpeg"
            },
            ]

前端想把data去了,只想显示

 [
        {
            "doid": 350792,
            "uid": 1117860,
            "message": "正面有什么变化吗?一个是12月份 ,一个是现在。",
            },
            "pre_home_doing_image": {
                "id": 473257,
                "doid": 350792,
                "url": "/B599765A-8B32-44B0-967A-818366357038.jpeg"
            },
            ]

谢谢大家

什么时候开始都不晚,学到老
《L03 构架 API 服务器》
你将学到如 RESTFul 设计风格、PostMan 的使用、OAuth 流程,JWT 概念及使用 和 API 开发相关的进阶知识。
《G01 Go 实战入门》
从零开始带你一步步开发一个 Go 博客项目,让你在最短的时间内学会使用 Go 进行编码。项目结构很大程度上参考了 Laravel。
最佳答案

可以的,具体可查阅文档->Eloquent->API资源->数据包裹:API 资源《Laravel 9 中文文档》App\Providers\AppServiceProvider文件中,调用Illuminate\Http\Resources\Json\JsonResource,并在boot方法中调用withoutWrapping方法

use Illuminate\Http\Resources\Json\JsonResource;
...
 public function boot()
    {
        JsonResource::withoutWrapping();
    }

希望可以帮到你

2年前 评论
芝麻开门 (楼主) 2年前
讨论数量: 4

论坛上搜索了一下,找到这个,但不知道怎么改... 问答:这东西只能用在 API 里直接 return 吗? $resource->response()->getData(true); 谢谢大家~

2年前 评论

可以的,具体可查阅文档->Eloquent->API资源->数据包裹:API 资源《Laravel 9 中文文档》App\Providers\AppServiceProvider文件中,调用Illuminate\Http\Resources\Json\JsonResource,并在boot方法中调用withoutWrapping方法

use Illuminate\Http\Resources\Json\JsonResource;
...
 public function boot()
    {
        JsonResource::withoutWrapping();
    }

希望可以帮到你

2年前 评论
芝麻开门 (楼主) 2年前

既然有前端,分页就自己写一下也是可以的,不一定非要用laravel封装好的,laravel封装的分页方法更多的是为了配合blade模板使用的。
还有可以使用response重新封装一下返回响应数据。可以处理一些特殊情况和根据异常自定义状态码和错误码。
一般来说,data这个字段是需要的。特别是接口状态返回永远是200的时候。
类似以下做法:

    // 接口请求成功
    public function success($data = [], $code = 200): JsonResponse
    {
        $response['data'] = $data;
        $response['code'] = $code;
        $response['message'] = 'OK';
        if (!$data) {
            unset($response['data']);
        }
        return response()->json($response);
    }

    // 常见错误请求
    // 404    未找到(请求资源不存在)
    // 401    未认证 (需要登录)
    // 403    没有权限
    // 400    错误的请求(URL 或参数不正确)
    // 422    验证失败
    // 500    服务器错误
    public function error($e): JsonResponse
    {
        if ($e->getCode() > 1000) {
            return response()->json(['message' => $e->getMessage(), 'exception' => '', 'code' => $e->getCode()]);
        }
        return response()->json([
            'message' => '服务错误,请将错误信息告知我们!',
            'exception' => $e->getMessage(),
            'line' => $e->getLine(),
            'file' => $e->getFile(),
            'code' => $e->getCode()
        ]);
    }
2年前 评论

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!