API Resources

This commit is contained in:
2020-08-08 20:45:11 +05:00
parent 6103cf19ec
commit b958ab757f
3 changed files with 76 additions and 0 deletions

View File

@@ -0,0 +1,30 @@
<?php
namespace App\Http\Resources;
use Illuminate\Http\Resources\Json\JsonResource;
use App\Http\Resources\SourceResource;
use App\Http\Resources\TopicResource;
class ArticleResource extends JsonResource
{
/**
* Transform the resource into an array.
*
* @param \Illuminate\Http\Request $request
* @return array
*/
public function toArray($request)
{
return [
"id" => $this->id,
"title" => $this->title,
"author" => $this->author,
"featured_image" => $this->featured_image,
"body" => $this->body,
"source" => new SourceResource($this->source),
"topics" => TopicResource::collection($this->topics),
"published_date" => $this->published_date
];
}
}