Laravel Fractal does not return meta from include relationship
Asked Answered
S

1

6

This is my PostTransformer where I have included relationship

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();
    }

}

This should result with a relationship of comments with meta as well. But the issue here is that I did not get meta in inlcuded relationship. If someone can help me out in this.

Strove answered 29/11, 2016 at 7:2 Comment(0)
A
0

This is a known problem with Fractal.

Issue

https://github.com/thephpleague/fractal/issues/339

Pull Request (Open)

There's an open pull request to include meta for relationships.

https://github.com/thephpleague/fractal/pull/350

Apteryx answered 7/4, 2017 at 9:23 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.