Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@uno-de-piera
Created May 18, 2018 11:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save uno-de-piera/e6a0f264cd512e7a5ca81577c92ca1c3 to your computer and use it in GitHub Desktop.
Save uno-de-piera/e6a0f264cd512e7a5ca81577c92ca1c3 to your computer and use it in GitHub Desktop.
<?php
namespace App\Http\Resources;
use Illuminate\Http\Resources\Json\Resource;
class CategoryResource extends Resource
{
/**
* Transform the resource into an array.
*
* @param \Illuminate\Http\Request
* @return array
*/
public function toArray($request)
{
return [
'id' => $this->id,
'name' => $this->name
];
}
}
<?php
namespace App\Http\Resources;
use App\Category;
use Illuminate\Http\Resources\Json\Resource;
class PostResource extends Resource
{
/**
* Transform the resource into an array.
*
* @param \Illuminate\Http\Request
* @return array
*/
public function toArray($request)
{
return [
"title" => $this->title,
"body" => $this->body,
'categories' => CategoryResource::collection($this->categories)
];
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment