laravel api resource 返回数组

不解释了,有需要的人会懂。

<?php

namespace App\Http\Resources;

use Illuminate\Http\Request;
use Illuminate\Http\Resources\Json\ResourceCollection;

class GuestbookCollection extends ResourceCollection
{
    /**
     * Transform the resource collection into an array.
     *
     * @return array<int|string, mixed>
     */
    public function toArray(Request $request): array
    {
        return $this->collection->map(function ($item){
            return [
                'username' => $item->user->username,
                'content' => $item->content,
                'created_at' => $item->created_at->format('Y-m-d H:i:s'),
                'reply_content' => $item->reply->content,
                'id' => $item->id,

            ];
        })->toArray();
    }
}

评论

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注