mardi 30 mai 2017

Laravel - use years as pagination numbers

I am on Laravel 5.1.45 (LTS). One club has many events.

I want to display a page which lists all the events for the current year (the default page). At the bottom at the page I want the links for the previous and for the next years (one link per page and one page per year).

This is my PublicController:

public function index()
{
            $events = Event::with('Club')
                                    ->orderBy('date', 'desc')
                                    ->get()
                                    ->groupBy(function($date) {
                                            return Carbon::parse($date->date)->format('Y');
                                    });
            $currentPage = Paginator::resolveCurrentPage() - 1;
            $perPage = 1;
            $currentPageSearchResults = $events->slice($currentPage * $perPage, $perPage)->all();
            $paginator = new LengthAwarePaginator($currentPageSearchResults, count($events), $perPage);
            return view('events.index', ['events' => $paginator]);
}

And this is my view:

@foreach ($events as $year)
    @foreach ($year as $object)
            
            
    @endforeach
@endforeach

{!! $events->setPath('/events/events')->appends(Request::except('page'))->render() !!}

This is working; I get all the events for the year in one page and one page for each year.

However, the links at the bottom of the page are, of course, in the form of, for example, '?page=2' for the 2016 events.

What I would like to have is a link in the form of '?page=YEAR'. And, if there are already events listed for the next year, to land on the page with the events for the current year if no parameter has been added to the URL.

Is that even possible?



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire