I'm on mobile now, not at my saved links. But if you do a Google search on laravel paginate collection you will find some previous answers I've given one myself, sorry again not at links right now.
Nov 30, 2019
9
Level 1
Paginate collection
Hi, is it possible to paginate colletion in Laravel ?
I've 5 tables : markets, benefit_market, benefits, benefit_item, items and these 3 models behind.
Markets have multiple benefits and benefits have multiple items
//Market.php
class Market extends Model
{
public function benefits()
{
return $this->belongsToMany(Benefit::class);
}
public function getItemsAttribute()
{
return $this->benefits
->pluck('items')
->collapse();
}
}
//Benefit.php
class Benefit extends Model
{
public function items()
{
return $this->belongsToMany(Item::class);
}
public function markets()
{
return $this->belongsToMany(Market::class);
}
}
//Item.php
class Item extends Model
{
public function benefits()
{
return $this->belongsToMany(Benefit::class);
}
public function getMarketsAttribute()
{
return $this->benefits
->pluck('markets')
->collapse()
->unique('id');
}
}
Since a market, i access to all items of all benefits for given market with : $market->items.
But now, I'ld like paginate this result and i don't know how do that...
//MarketController.php
class MarketController extends Controller
{
public function show(Market $market)
{
$items = $market->items->paginate(10); // it doesn't work !
return view('pages.markets.show', compact('market', 'items'));
}
}
Hope someone can help me. Thanks. :)
Please or to participate in this conversation.