Laravel分形变换器 - 没有得到包括

时间:2017-09-24 22:12:12

标签: laravel thephpleague-fractal

我在laravel 5.3中使用了分形包。我有一个与视频有hasMany关系的玩家模型,因此一个玩家有很多视频。

播放器型号:

public function videos()
  {
      return $this->hasMany(Video::class);
  }

视频型号:

public function player()
{
    return $this->belongsTo(Player::class);
}

我正在尝试发送播放器数据并包含视频数据。这是控制器中的代码:

            $player  =  fractal()
                       ->item($player)
                       ->parseIncludes(['videos'])
                       ->transformWith(new PlayerTransformer)
                       ->toArray();

这是PlayerTransformer

<?php

namespace App\Transformer;

use App\Player;

class PlayerTransformer extends \League\Fractal\TransformerAbstract
{
    protected $availableIncludes = [
        'videos'
    ];

    public function transform(Player $player)
    {
        return [
          'id' => $player->id,
          'first_name' => $player->first_name,
          'last_name' => $player->last_name,
          'image' => $player->image_filename,
          'club' => $player->club,
          'birthday' => $player->birthday,
        ];
    }

    public function includeVideos(Player $player)
    {
        $videos = $player->videos()->where('processed', 1)->get();

        return $this->collection($videos, new VideoTransformer);
    }
}

我正在获取播放器数据,但我没有为视频获取任何内容。这不起作用,但如果尝试使用视频,甚至更奇怪的是,在我的变换器中我甚至没有包含功能:

<?php

namespace App\Transformer;

use App\Video;

class VideoTransformer extends \League\Fractal\TransformerAbstract
{
    public function transform(Video $video)
    {
        return [
          'id' => $video->id,
          'uid' => $video->uid,
          'title' => $video->title,
          'image' => $video->getThumbnail(),
          'description' => $video->description,
          'views' => $video->viewCount(),
          'created_at' => $video->created_at->diffForHumans(),
        ];
    }
}

但是,当我尝试在那里包含播放器数据时,这有效:

              $videos = fractal()
                        ->collection($videos)
                        ->parseIncludes(['player'])
                        ->transformWith(new VideoTransformer)
                        ->toArray();

我已尝试将相同的设置应用于视频转换器并移除播放器变换器中的$availableIncludesincludeVideos,但它再次无法在那里工作。我做错了什么?

0 个答案:

没有答案