for testing and deploying your application
for finding and fixing issues
for empowering human code reviews
<?php
namespace App\Http\Resources;
use Illuminate\Http\Request;
use Illuminate\Http\Resources\Json\JsonResource;
/**
* Book API Resource for transforming book data.
*/
class BookResource extends JsonResource
{
* Transform the resource into an array.
*
* @return array<string, mixed>
public function toArray(Request $request): array
return [
'id' => $this->id,
id
App\Http\Resources\BookResource
__get
'title' => $this->title,
title
'author' => $this->author,
author
'asin' => $this->asin,
asin
'isbn' => $this->isbn,
isbn
'ean' => $this->ean,
ean
'url' => $this->url,
url
'salesrank' => $this->salesrank,
salesrank
'publisher' => $this->publisher,
publisher
'publishdate' => $this->publishdate,
publishdate
'pages' => $this->pages,
pages
'overview' => $this->overview,
overview
'genre' => $this->genre,
genre
'cover' => $this->cover ? true : false,
cover
'cover_url' => $this->getCoverUrl(),
'created_at' => $this->created_at?->toIso8601String(),
created_at
'updated_at' => $this->updated_at?->toIso8601String(),
updated_at
];
}
* Get the cover image URL.
protected function getCoverUrl(): ?string
if (! $this->cover) {
return null;
$coverPath = storage_path('covers/book/'.$this->id.'.jpg');
if (file_exists($coverPath)) {
return url('/covers/book/'.$this->id.'.jpg');