请求所有数据的格式化结果

Format result for requesting all data

我的控制器有 index()show()

{
    /**
     * Display a listing of the resource.
     *
     * @return \App\Http\Resources\MyRessource
     */
    public function index()
    {
        //???
    }


    /**
     * Display the specified resource.
     *
     * @param  \App\Models\Test  $test
     * @return \App\Http\Resources\TestRessource
     */
    public function show(Test $test)
    {
        return new \App\Http\Resources\TestRessource($test);
    }
}

在我的资源中,show() 具有我想要的 return 格式,因此 http://127.0.0.1/Test/1 的结果是 ID 1,格式为 JSON。

{
    "data": {
        "id": 1,
        "ref": "0103573026466442101007175850",
        "tax": null,
        "date_in": "2021-10-08T12:37:05.000000Z",
        "date_out": "2021-10-11T08:02:17.000000Z"
    }
}

我希望通过使用我的资源以相同的方式 index() 到 return。 当我在 http://127.0.0.1/Test 上执行 index() 时,它 return 是我所有的数据,但不是我想要的格式 JSON。

资源码:

{
    /**
     * Transform the resource into an array.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return array|\Illuminate\Contracts\Support\Arrayable|\JsonSerializable
     */
    public function toArray($request)
    {
        return [
            "id" => $this->id,
            "ref" => $this->ref,
            "tax" => $this->tax,
            "date_in" => $this->date_in,
            "date_out" => $this->date_out
        ];
    }

index() 上按照 docs 中的方式进行操作。

return TestRessource::collection(Test::all());