dongyinju5977 2016-01-06 02:27
浏览 34
已采纳

分形解析包括不工作

I'm using the Laravel framework for my web app, eloquent models for data and Fractal to transform some data.

I want to use the parseIncludes functionality of fractal but I can't seem to get it working despite following the docs.

Here's my code:

StudentTransformer.php

class StudentTransformer extends Fractal\TransformerAbstract
{

    protected $availableIncludes = [
        'course'
    ];

    public function transform(Student $student)
    {
        return [
            'name'           => $student->name,
            // other attributes
        ];
    }

    public function includeCourse(Student $student)
    {
        $course = $student->course;

        return $this->item($course, new CourseTransformer);
    }

}

CourseTransformer.php

class CourseTransformer extends Fractal\TransformerAbstract
{
    public function transform(Course $course)
    {
        return [
            'name' => $course->name
            // other attributes
        ];
    }
}

In one of my controllers:

$student = App\Models\Student::first();

$fractal = new \League\Fractal\Manager();

$fractal->parseIncludes('/student?include=course');

$fractal->setSerializer(new \League\Fractal\Serializer\ArraySerializer());

$response = new \League\Fractal\Resource\Item($student, new \App\Transformers\Models\StudentTransformer);

return response()->json($fractal->createData($response)->toArray());

Also, when I remove the availableIncludes from the StudentTransformer and use defaultIncludes instead, like so:

protected $defaultIncludes = [
    'course'
];

It works just fine?! No idea why! Any help would be appreciated.

展开全部

  • 写回答

1条回答 默认 最新

  • doudi4621 2016-01-06 02:44
    关注

    Fixed it. For the benefit of others, the issue was here:

    $fractal->parseIncludes('/student?include=course');
    

    It should just be:

    $fractal->parseIncludes('course');
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
编辑
预览

报告相同问题?

悬赏问题

  • ¥15 全志t113i启动qt应用程序提示internal error
  • ¥15 ensp可以看看嘛.
  • ¥80 51单片机C语言代码解决单片机为AT89C52是清翔单片机
  • ¥60 优博讯DT50高通安卓11系统刷完机自动进去fastboot模式
  • ¥15 minist数字识别
  • ¥15 在安装gym库的pygame时遇到问题,不知道如何解决
  • ¥20 uniapp中的webview 使用的是本地的vue页面,在模拟器上显示无法打开
  • ¥15 网上下载的3DMAX模型,不显示贴图怎么办
  • ¥15 关于#stm32#的问题:寻找一块开发版,作为智能化割草机的控制模块和树莓派主板相连,要求:最低可控制 3 个电机(两个驱动电机,1 个割草电机),其次可以与树莓派主板相连电机照片如下:
  • ¥15 Mac(标签-IDE|关键词-File) idea
手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部