thephpleague / fractal

Output complex, flexible, AJAX/RESTful data structures.
fractal.thephpleague.com
MIT License
3.52k stars 352 forks source link

How to transform when Parent Child Relation in same Table #523

Closed singleseeker closed 3 years ago

singleseeker commented 3 years ago

Like

books

id 
title
content
parent_id

<?php namespace App\Transformer;

use Acme\Model\Book;
use League\Fractal\TransformerAbstract;

class BookTransformer extends TransformerAbstract
{
    /**
     * List of resources to automatically include
     *
     * @var array
     */
    protected $defaultIncludes = [
        'parentBook'
    ];

    // ....

    /**
     * Include Parent Book
     *
     * @param Book $book
     * @return \League\Fractal\Resource\Item
     */
    public function includeParentBook(Book $book)
    {
        $parentBook = $book->parent;

        return $this->item($parentBook, .....); // how ?
    }
}
singleseeker commented 3 years ago

Just use BookTransformer self