Bachar ELkarni
Bachar ELkarni

Reputation: 73

Get items of all the pages of a paginator in Laravel

I am stuck with a part were I have some paginated data displayed and I want that data to be sorted when I press on a button in Laravel 9 but the thing is that I can't find any ways to retrieve all the items of the paginator (I only get the items of the first page).

What I did:

I made a form that have a button that submits the form and triggers the sorting method in that form I added a hidden input that has as value the paginator instance in my Blade view.

This paginator instance has two pages each one have two items:

$events = Event::where('endingAt', '>', Carbon::now('GMT+1'))->paginate(2);

To see it clearly here's a dd of that $events variable

^ array:2 [▼
  0 => {#294 ▼
    +"id": 18
    +"title": "Gaming Hackathon"
    +"object": "This hackathon is a competition between different selected teams with an objective of building a full functional game in 3 days!!"
    +"startingAt": "2022-08-12 06:30:00"
    +"endingAt": "2022-08-14 21:00:00"
    +"location": "Kabukichō Ichiban-gai, Shinjuku, Tokyo, Japan"
    +"room": "III-5"
    +"created_at": "2022-08-11T11:07:47.000000Z"
    +"updated_at": "2022-08-11T11:07:47.000000Z"
  }
  1 => {#293 ▼
    +"id": 19
    +"title": "Soft Skills Course"
    +"object": "This event is a course available for our employees to further develop their soft skills and especially negotiation and communication skills"
    +"startingAt": "2022-08-12 09:00:00"
    +"endingAt": "2022-09-12 18:40:00"
    +"location": "11, Amdl Rabat, Rue Al Kayraouane , Hassan, Rabat, Morocco"
    +"room": "Open Space"
    +"created_at": "2022-08-11T11:10:30.000000Z"
    +"updated_at": "2022-08-11T11:10:30.000000Z"
  }
]

It only returns 2 items out of 4 which means that it only returns the paginator's first page's items. How can I make sure that I get the items of all the paginator's pages?

Upvotes: 0

Views: 1519

Answers (2)

Bachar ELkarni
Bachar ELkarni

Reputation: 73

I've solved it by pushing query results to the session before paginating them, I know it would hinder the website's performance but it's just a temporary fix, I guess I will create a temp table in the database and store/remove that data in it whenever I need.

That's the best solution I thought of, if you got a better solution please let me know :D

Upvotes: 0

BobB
BobB

Reputation: 762

if you want all the items you don't want to paginate so just call get instaed of paginate

$events = Event::where('endingAt', '>', Carbon::now('GMT+1'))->get();

Upvotes: 1

Related Questions