dimanche 25 mars 2018

Return multiple in index method - Laravel Api Resources

I'm trying to fetch latest articles, trending articles and categories on the index method

Here is the controller code

public function index() {
  $articles = Article::latest()->paginate(10);
  $trendingarticles = Article::all()->orderBy('pageviews');
  $categories = ArticleCategory::all();

  return ArticleResource::collection($articles);
}

How to return trendingarticles and categories as collection too? Also do i need to add anything more to the ArticleResource

public function toArray($request)
    {
        return [
          'id' => $this->id,
          'article_url' => '/articles/'.$this->category->slug.'/'.$this->slug,
          'category' => $this->category->name,
          'category_slug' => $this->category->slug,
          'category_url' => '/articles/'.$this->category->slug,
          'image' => url('/uploads/articles').'/articles/'.$this->image,
          'author' => $this->creator->name,
          'created' => Carbon::parse($this->created_at)->diffForHumans(),
          'updated' => Carbon::parse($this->updated_at)->diffForHumans(),
          'views' => $this->page_views,
          'favoriters' => $this->favoriters()->count(),
          'title' => $this->title,
          'subtitle' => $this->subtitle,
          'description' => $this->content,
          'links' => [
              'self' => 'link-value',
          ],
        ];
    }



from Newest questions tagged laravel-5 - Stack Overflow https://ift.tt/2uiqNjB
via IFTTT

Aucun commentaire:

Enregistrer un commentaire