小编sam*_*har的帖子

Laravel Fractal不会从包含关系中返回元

这是我PostTransformer加入关系的地方

public function includeComments(Post $post)
{
    if (($post->is_paid == 1 && $post->haspaid == 1) || ($post->author == $this->params) || ($post->is_paid == 0)){
        $comments = Comment::where('post', $post->id)
            ->where('is_blocked', '=', 0)
            ->select('id',
                'text',
                'author',
                'post',
                'created_at',
                'updated_at',
                'book_id',
                DB::raw("if(comments.author=$this->params,'true','false') as isauthor"))
            ->orderBy('created_at', 'ASC')
            ->paginate(5);
        $commentTransformer = new CommentTransformer($this->params);
        $commentResource = $this->collection($comments, $commentTransformer, 'comment')->setMeta(['total'=>count($comments)]);
        return $commentResource;
    } elseif ($post->is_paid == 1 && $post->haspaid == 0) {
        return $this->null();
    }

}
Run Code Online (Sandbox Code Playgroud)

这应该与meta的评论关系产生.但这里的问题是我没有得到包含关系的meta.如果有人可以帮我解决这个问题.

php json-api laravel-5.3 thephpleague thephpleague-fractal

6
推荐指数
1
解决办法
391
查看次数