What you get in your call is a Collection object if I'm not mistaken, and you're free to pass the Collection as the first argument to the Paginator. Notice line 40.
@RachidLaasri how exactly is it not working for you? What output do you get?
@RachidLaasri it's because get() method returns a Collection instance, and there's no paginate() method on Collection instance. The groupBy() you're calling is actually performed on the Collection instance, after all of the data has been loaded.
Since paginate() method on the Eloquent model returns a LengthAwarePaginator instance, can't you do the same?
$data = Post::latest()
->get()
->groupBy(function($date) {
return Carbon::parse($date->created_at)->format('l');
});
return new LengthAwarePaginator($data, /** other params */);
@JarekTkaczyk i didn't understand what you meant, what i want to do is show the posts on the home page, i want the user to be able to see the posts grouped by days with pagination, from newest to oldest...
@RachidLaasri Well, I asked what's the pagination base. You said the date, so I suppose this:
you want N dates per page, no matter how many posts there are for each date
you want N dates with posts, not N consecutive dates
That said, you want to first get the N dates (distinct) ordered desc, then you get the real result - your posts, that were published on that N dates. Next, you just group the result collection by date and you're good. Should I explain it in code?
@RachidLaasri Assuimng MySQL and created_at timestamp as your field (if you're using date type, then no need for raw additions):
$dates = Post::selectRaw('date(created_at) as date')->latest()->distinct()->paginate()->lists('date');
$posts = Post::whereIn(DB::raw('date(created_at)'), $dates)->get()->groupBy( YOUR CLOSURE HERE );