LARAVEL

resource and collection

11.03.2024 1 dəq oxuma

  

HomeAboutChronologyResource.php
<?php

namespace App\Http\Resources\Home;

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

class HomeAboutChronologyResource extends JsonResource
{
    /**
     * Transform the resource into an array.
     *
     * @return array<string, mixed>
     */
    public function toArray(Request $request): array
    {
        return [
            'id' => $this->id,
            'date'=>$this->date,
            'content'=>$this->content,
        ];
    }
}
HomeAboutChronologyCollection.php
<?php

namespace App\Http\Resources\Home;

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

class HomeAboutChronologyCollection extends ResourceCollection
{
    /**
     * Transform the resource collection into an array.
     *
     * @return array<int|string, mixed>
     */
    public function toArray(Request $request): array
    {
        return [
            $this->collection->transform(function ($chronlogy){
                return new HomeAboutChronologyResource($chronlogy);
            })
        ];
    }
}

 

Digər dildə: EN