PostResourceWithOptionalRelationship.php 523 B

123456789101112131415161718
  1. <?php
  2. namespace Illuminate\Tests\Integration\Http\Fixtures;
  3. class PostResourceWithOptionalRelationship extends PostResource
  4. {
  5. public function toArray($request)
  6. {
  7. return [
  8. 'id' => $this->id,
  9. 'comments' => new CommentCollection($this->whenLoaded('comments')),
  10. 'author' => new AuthorResource($this->whenLoaded('author')),
  11. 'author_name' => $this->whenLoaded('author', function () {
  12. return $this->author->name;
  13. }),
  14. ];
  15. }
  16. }