CRM/backend/app/Http/Resources/NoteResource.php

35 خطوط
1.1 KiB
PHP

<?php
namespace App\Http\Resources;
use Illuminate\Http\Request;
use Illuminate\Http\Resources\Json\JsonResource;
use Illuminate\Support\Facades\Gate;
class NoteResource extends JsonResource
{
public static $wrap = null;
public function toArray(Request $request): array
{
$gate = Gate::forUser($request->user());
return [
'id' => $this->id,
'content' => $this->content,
'type' => $this->type,
'visibility' => $this->visibility,
'is_pinned' => (bool) $this->is_pinned,
'author' => $this->whenLoaded('user', fn () => $this->user?->only(['id', 'name'])),
'edited_at' => $this->edited_at?->toISOString(),
'created_at' => $this->created_at?->toISOString(),
'updated_at' => $this->updated_at?->toISOString(),
'capabilities' => [
'edit' => $gate->allows('update', $this->resource),
'delete' => $gate->allows('delete', $this->resource),
'pin' => $gate->allows('pin', $this->resource),
],
];
}
}