samedi 26 mars 2016

Laravel fractal return count from include

I am using Fractal library with Laravel 5 to serialize and tranform data to JSON. I stuck on returning count of included object.

Article model:

  public function articleVotes() {
    return $this->hasMany('App\ArticleVote');
  }

  public function articleVotesCount() {
    return $this->hasOne('App\ArticleVote')->selectRaw('article_id, count(*) as aggregate')->groupBy('article_id');
  }

I need to show this count in transformer:

public function transform(Article $article) {
  return [
    'id'         => $article->id,
    'title'      => $article->title,
    'body'       => $article->body,
    'created_at' => $article->created_at->toDateTimeString(),
    'votes'      => $article->articleVotesCount // !! - not working
  ];
}

public function includeUser(Article $article) {
  return $this->item($article->user, new UserTransformer);
}

public function includeArticleVotes ???? - not working

Did anyone have similar problem?



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire