Last active
March 26, 2025 12:18
-
-
Save atomjoy/96ce272d121dca06ce3c987c328d4d6d to your computer and use it in GitHub Desktop.
Add additional data to resource.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\Http\Resources; | |
use App\Models\Admin; | |
use Illuminate\Http\Request; | |
use Illuminate\Http\Resources\Json\JsonResource; | |
class ArticleResource extends JsonResource | |
{ | |
/** | |
* The "data" wrapper that should be applied. | |
* | |
* @var string | |
*/ | |
public static $wrap = 'data'; | |
/** | |
* Transform the resource into an array. | |
* | |
* @return array<string, mixed> | |
*/ | |
public function toArray(Request $request): array | |
{ | |
$admin = Admin::find($this->admin_id); | |
// Additional data mixed | |
$admin->current_article_id = $this->id; | |
return [ | |
'author' => new ArticleAuthorResource($admin), | |
'admin' => ArticleAuthorResource::make($admin), | |
'id' => $this->id, | |
'title' => $this->title, | |
'slug' => $this->slug, | |
'excerpt' => $this->excerpt, | |
'content_html' => $this->content_html, | |
'content_cleaned' => $this->content_cleaned, | |
'image' => $this->image, | |
'views' => $this->views, | |
'meta_seo' => $this->meta_seo, | |
'schema_seo' => $this->schema_seo, | |
'created_at' => $this->created_at->format('Y-m-d H:i:s'), | |
'updated_at' => $this->updated_at->format('Y-m-d H:i:s'), | |
'published_at' => $this->published_at, | |
'tags' => $this->tags, | |
'categories' => $this->categories, | |
'comments' => $this->comments()->latest()->limit(5), | |
]; | |
// return parent::toArray($request); | |
} | |
public function with($request) | |
{ | |
return ['status' => 'success']; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\Http\Resources; | |
use App\Models\Admin; | |
use Illuminate\Http\Request; | |
use Illuminate\Http\Resources\Json\JsonResource; | |
class ArticleMiniResource extends JsonResource | |
{ | |
/** | |
* Transform the resource into an array. | |
* | |
* @return array<string, mixed> | |
*/ | |
public function toArray(Request $request): array | |
{ | |
return [ | |
'id' => $this->id, | |
'title' => $this->title, | |
'slug' => $this->slug, | |
'excerpt' => $this->excerpt, | |
'content_html' => $this->content_html, | |
'content_cleaned' => $this->content_cleaned, | |
'image' => $this->image, | |
'views' => $this->views, | |
'published_at' => $this->published_at, | |
'author' => new ArticleAuthorMiniResource(Admin::find($this->admin_id)), | |
// 'author' => Admin::select('name', 'avatar', 'id')->find($this->admin_id), | |
]; | |
// return parent::toArray($request); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\Http\Resources; | |
use Illuminate\Http\Request; | |
use Illuminate\Http\Resources\Json\JsonResource; | |
class ProductResource extends JsonResource | |
{ | |
public function toArray(Request $request): array | |
{ | |
return [ | |
'id' => $this->id, | |
'name' => $this->name, | |
'slug' => $this->slug, | |
'price' => $this->price, | |
// Include full description only in detailed view | |
'description' => $this->when( | |
$request->route()->named('products.show'), | |
$this->description, | |
$this->excerpt | |
), | |
// Include stock info for authenticated users | |
'stock_level' => $this->when( | |
$request->user()?->can('view-inventory'), | |
$this->stock_count | |
), | |
// Include relationships when loaded | |
'category' => new CategoryResource($this->whenLoaded('category')), | |
// Include counts conditionally | |
'reviews_count' => $this->when( | |
$request->include_counts, | |
$this->reviews_count | |
), | |
// Include admin data | |
'profit_margin' => $this->when( | |
$request->user()?->isAdmin(), | |
fn() => $this->calculateProfitMargin() | |
), | |
]; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use Illuminate\Http\Request; | |
use Illuminate\Http\Resources\Json\JsonResource; | |
class AdminResource extends JsonResource | |
{ | |
public function toArray(Request $request): array | |
{ | |
return [ | |
'id' => $this->id, | |
'name' => $this->name, | |
'email' => $this->when($request->user()->isAdmin(), $this->email), | |
]; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\Traits; | |
use App\Http\Resources\TeacherResource; | |
// Teacher for StudentResource | |
trait WithTeacher | |
{ | |
public function with(Request $request): array | |
{ | |
return [ | |
'teacher' => TeacherResource::make($this->resource) | |
]; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment