0

Hello everyone I'm trying to make pagination in Laravel 4 but my code doesn't work. I have controller with action:

public function getSingleProduct($prodName, $id)
    {               
        $singleProduct = Product::getOne($id);
        $getAllReviews = Review::getAllBelongsToProduct($id);
        $this->layout->content = View::make('products.single')
                                     ->with('reviews', $getAllReviews)
                                     ->with('products', $singleProduct);
    }

and I want to paginate getAllReviews (5 per page). I tried like this: $getAllReviews = Review::getAllBelongsToProduct($id)->paginate(5); but it doesn't work for me. Here is also my Review model

public static function getAllBelongsToProduct($id) {
     return self::where('product_id', '=', $id)
                ->join('products', 'reviews.product_id', '=', 'products.id')                    
                ->select('reviews.*', 'products.photo')
                ->orderBy('created_at', 'desc')
                ->get();
}

Where I have a mistake?

Zolax
  • 91
  • 3
  • 12

1 Answers1

1

Instead of that static method on your model use query scope, this will be flexible:

// Review model
public function scopeForProduct($query, $id)
{
    $query->where('product_id', $id);
}

public function scopeWithProductPhoto($query)
{
    $query->join('products', 'reviews.product_id', '=', 'products.id')                    
      ->select('reviews.*', 'products.photo');
}

Then use it:

// get all
$reviews = Review::forProduct($id)->withProductPhoto()->latest()->get();

// get paginated
$reviews = Review::forProduct($id)->withProductPhoto()->latest()->paginate(5);

latest is built-in method for orderBy('created_at', 'desc').

If you want to have just a single call in your controller, then chain the above and wrap it in methods on your model.

Jarek Tkaczyk
  • 78,987
  • 25
  • 159
  • 157
  • Thank you very much so instead of static method should I always use query scope? – Zolax Jul 19 '14 at 15:18
  • It's not that you should always use query scopes, and never use static methods. However the way I showed you is much more flexible. And you need to know, that every static (or static looking `::`) call to Eloquent Model instantiates it anyway. – Jarek Tkaczyk Jul 19 '14 at 16:24
  • Man can I ask you one more question? How can I get the latest review to each product and display it in page where are all products? – Zolax Jul 20 '14 at 08:57
  • 1
    Use `hasOne` relationship and eager load single review per product, just like in this case: http://stackoverflow.com/questions/24343738/getting-just-the-latest-value-on-a-joined-table-with-eloquent#answer-24350807 – Jarek Tkaczyk Jul 20 '14 at 09:03
  • For me when I did in Product model `public function latestPrice() { return $this->hasOne('Review')->latest(); } ` It doesnt work for me. I need latest review for each product in category page. Should I ask new question? – Zolax Jul 21 '14 at 11:06
  • @Zolax What do you mean? – Jarek Tkaczyk Jul 21 '14 at 11:08
  • I see that you are from Poland have you any communicator? I will explain it. – Zolax Jul 21 '14 at 11:19
  • catch me in #laravel @freenode – Jarek Tkaczyk Jul 21 '14 at 11:21
  • I can't see you my nick is Zolax. – Zolax Jul 21 '14 at 11:23