I'm working on Laravel project that has two models: Offer
belongsTo Category
.
It's easy to paginate the results when I'm retrieving all records:
$offers = Offer::paginate(10);
But when I'm trying to just retrieve Offers
that has specific Category
, It just do not work:
$category = Category::whereId($category_id)->with('offers')->first()->paginate(10);
and I get this error:
Undefined property: Illuminate\Pagination\Paginator::$offers
UPDATE:
I've solved it by replacing the second code with this:
$category = Category::find($category_id);
$offers = $category->offers()->paginate(10);