Skip to content

Commit

Permalink
feat: create some resource to get data for blog posts
Browse files Browse the repository at this point in the history
  • Loading branch information
tanhongit committed Dec 5, 2024
1 parent e9ed92c commit 789bd7c
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 0 deletions.
25 changes: 25 additions & 0 deletions src/Http/Resources/AuthorResource.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

namespace CSlant\Blog\Api\Http\Resources;

use Botble\Blog\Models\Category;
use CSlant\Blog\Core\Models\User;
use Illuminate\Http\Resources\Json\JsonResource;

/**
* @mixin Category
*/
class AuthorResource extends JsonResource
{
/** */
public function toArray($request): array
{
/** @var User $this */
return [
'id' => $this->id,
'first_name' => $this->first_name,
'last_name' => $this->last_name,
'image' => $this->avatar_url,
];
}
}
25 changes: 25 additions & 0 deletions src/Http/Resources/CategoryResource.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

namespace CSlant\Blog\Api\Http\Resources;

use Botble\Blog\Models\Category;
use Illuminate\Http\Resources\Json\JsonResource;

/**
* @mixin Category
*/
class CategoryResource extends JsonResource
{
/** */
public function toArray($request): array
{
/** @var Category $this */
return [
'id' => $this->id,
'name' => $this->name,
'slug' => $this->slug,
'url' => $this->url,
'description' => $this->description,
];
}
}
31 changes: 31 additions & 0 deletions src/Http/Resources/ListPostResource.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

namespace CSlant\Blog\Api\Http\Resources;

use Botble\Blog\Http\Resources\TagResource;
use Botble\Blog\Models\Post;
use Botble\Media\Facades\RvMedia;
use Botble\Blog\Http\Resources\ListPostResource as BaseListPostResource;

/**
* @mixin Post
*/
class ListPostResource extends BaseListPostResource
{
public function toArray($request): array
{
/** @var Post $this */
return [
'id' => $this->id,
'name' => $this->name,
'slug' => $this->slug,
'description' => $this->description,
'image' => $this->image ? RvMedia::url($this->image) : null,
'categories' => CategoryResource::collection($this->categories),
'tags' => TagResource::collection($this->tags),
'author' => AuthorResource::make($this->author),
'created_at' => $this->created_at,
'updated_at' => $this->updated_at,
];
}
}

0 comments on commit 789bd7c

Please sign in to comment.