thephpleague / fractal

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

[Question] How to transformer with has Many #342

Closed vinacms closed 2 years ago

vinacms commented 7 years ago

I have one category has many post, I want make transfomer with them.

Thank you!

catalinux commented 7 years ago

In your transformer you should define a method called: includePosts like this:

function includePosts(Category $category){
   return $this->collection($category->posts, new PostTransformer());
}

To be included in your reponse, in your request you need to add include=posts.

If you need to be included by default, you need to add posts in the $defaultIncludes array defined on the transformer

catalinux commented 7 years ago

These examples might help you: http://fractal.thephpleague.com/transformers/

matt-allan commented 7 years ago

@SieuNT If you got it working can you close this? thanks 😄

canon4ever commented 7 years ago

@catalinux, this is useful.

Here is my code:

//Controller
function category()
{
    $categories = Category::with('children')->get();
    return fractal()
         ->collection($categories, new CategoryTransformer())
         ->includeChildren()
         ->toArray();
}
//Transformer
namespace App\Http\Controllers\Api\Mobile\Transformer;

use League\Fractal\TransformerAbstract;
use App\Models\Shop\Category;

class CategoryTransformer extends TransformerAbstract
{
    protected $availableIncludes = [
        'children',
    ];

    public function transform(Category $category)
    {
        return [
            'id' => $category->id,
            'name' => $category->name,
            'thumb' => $category->thumb,
        ];
    }

    public function includeChildren(Category $category)
    {
        return $this->collection($category->children, new CategoryTransformer);
    }

}

After that, i got the result like this:

[
  {
    "id": 26,
    "name": "长乐生活方式",
    "thumb": "",
    "children": [
      {
        "id": 23,
        "name": "iRobot",
        "thumb": "/storage/images/2016_04/20160412174358_18674.jpg"
      },
      {
        "id": 21,
        "name": "平衡车",
        "thumb": "/storage/images/2016_04/20160412171040_37324.png"
      },
      {
        "id": 32,
        "name": "HHKB",
        "thumb": "/storage/images/2016_04/20160412174417_21052.jpg"
      }
    ]
  },
  {
    "id": 1,
    "name": "电脑",
    "thumb": "",
    "children": [
      {
        "id": 3,
        "name": "iPad",
        "thumb": "/storage/images/2016_04/20160412174442_13016.jpg"
      },
      {
        "id": 2,
        "name": "iMac/Mac pro",
        "thumb": "/storage/images/2016_04/20160412174454_81978.jpg"
      }
    ]
  },
  {
    "id": 25,
    "name": "手机",
    "thumb": "",
    "children": [
      {
        "id": 27,
        "name": "iPhone",
        "thumb": "/storage/images/2016_04/20160412174506_87585.jpg"
      },
      {
        "id": 28,
        "name": "小米",
        "thumb": "/storage/images/2016_04/20160412171017_71290.png"
      }
    ]
  },
  {
    "id": 36,
    "name": "休闲娱乐",
    "thumb": "",
    "children": [
      {
        "id": 35,
        "name": "Kindle",
        "thumb": "/storage/images/2016_04/20160412174520_10220.jpg"
      },
      {
        "id": 37,
        "name": "3DS",
        "thumb": "/storage/images/2016_04/20160412174531_81715.jpeg"
      },
      {
        "id": 38,
        "name": "Play Station",
        "thumb": "/storage/images/2016_04/20160412174543_35152.jpg"
      }
    ]
  }
]

The result is just what i wanted. Thanks for @catalinux

pengxuxu commented 7 years ago

@canon4ever good job!

huangzhihuang commented 7 years ago

@canon4ever 厉害了我的师

beingmate commented 7 years ago

nice,66666