I'm having trouble paginating my tag page which lists all the products that are associated to a tag (many to many). The tag page is found on slug. I have the lists of products associated with the tag ordered on the highest average rating from the reviews related to the products. Unfortunately, I cannot seem to get the paginate working. Does anyone know how I'd do this from the code below
Tables:
products: id, name, price, approved
tags: id, name, slug
-products_tag: id, product_id, tag_id
reviews: id, comment, rating, product_id
current code:
public function show($slug)
{
//
$tag = Tag::with(['products' => function ($query) {
return $query->where('approved', '=', 1)
->leftJoin('reviews', 'reviews.products_id', '=', 'products.id')
->select('products.*', DB::raw('AVG(rating) as ratings_average' ))
->groupBy('id')
->orderBy('ratings_average', 'DESC');}])->where('slug', $slug)->first();
return view('tags.show')->withTag($tag);
}
In the end of your query you call the method fisrt(), you need to you the method paginate() to generate the variables that you need.