timacdonald / json-api

A lightweight API resource for Laravel that helps you adhere to the JSON:API standard. Supports sparse fieldsets, compound documents, and more.
586 stars 40 forks source link

How to get from JsonResource to JsonApiResource #38

Closed rabol closed 1 year ago

rabol commented 1 year ago

Hi

I'm trying to switch from Laravel JsonResource to JsonApiResource and was under the impression that one could simply replace the JsonResource with JsonApiReource

"Declaration of App\Http\Resources\UserSignatureResource::toArray(Illuminate\Http\Request $request): array must be compatible with TiMacDonald\JsonApi\JsonApiResource::toArray($request)",

<?php

namespace App\Http\Resources;

use Illuminate\Http\Request;
use TiMacDonald\JsonApi\JsonApiResource;

/** @mixin \App\Models\UserSignature */
class UserSignatureResource extends JsonApiResource
{
    public function toArray(Request $request): array
    {
        return [
            'id' => $this->id,
            'name' => $this->name,
            'signature' => $this->signature,
            'user_id' => $this->user_id,
        ];
    }
}
rabol commented 1 year ago

Create

 public array $attributes = [ 
 ];

With the properties that you want to expose.