API 资源集合的如何像资源那样新增和减少返回的值

// 获取课程列表
public function getList(){
    return new CourseCateCollection(EduCourseCate::all());
}
    public function toArray($request)
    {
        return parent::toArray($request);
    }

上面是源码,如果我按照下面资源的这样写的话,会报错

public function toArray($request)
    {
        return [
            'id' => $this->id,
            'name' => $this->name,
            'email' => $this->email,
            'created_at' => $this->created_at,
            'updated_at' => $this->updated_at,
        ];

这样的问题,还如何处理?

附言 1  ·  5年前

解决了。看到了别人有提问这个问题的。

第一步的时候的代码,改成这个的一个返回就行了
UserResource::collection(User::all());

《L04 微信小程序从零到发布》
从小程序个人账户申请开始,带你一步步进行开发一个微信小程序,直到提交微信控制台上线发布。
《G01 Go 实战入门》
从零开始带你一步步开发一个 Go 博客项目,让你在最短的时间内学会使用 Go 进行编码。项目结构很大程度上参考了 Laravel。
最佳答案

就是不是直接使用api集合类,使用api资源,然后使用里面的,collection的方法,类似于下面的操作

    // 获取课程列表
    public function getList(){
        return CourseCateResource::collection(EduCourseCate::all());
    }
5年前 评论
讨论数量: 4

就是不是直接使用api集合类,使用api资源,然后使用里面的,collection的方法,类似于下面的操作

    // 获取课程列表
    public function getList(){
        return CourseCateResource::collection(EduCourseCate::all());
    }
5年前 评论

或者使用api资源类的时候 ,这么使用也是可以,反正就是借助api资源来对输出的结果,进行处理的一种方法

class CourseCateCollection extends ResourceCollection
{
    /**
     * Transform the resource collection into an array.
     *
     * @param  \Illuminate\Http\Request  $request
     * [@return](https://learnku.com/users/31554) array
     */
    public function toArray($request)
    {
        return [
            'data' => CourseCateResource::collection($this->collection),
            'links' => [
                'self' => 'link-value',
            ],
        ];
    }
}
5年前 评论
我也是醉了。再这里自问自答
跟炫耀一样
5年前 评论

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