Support the ongoing development of Laravel.io →
posted 9 years ago
Eloquent

I have a list of events.

These events have status updates.

I'm paginating the statuses 10 per page.

When the page is loaded the status updates are ordered by created_at desc.

I also have links to order by ascending or descending by passing ?sort=asc or ?sort=desc in the URL.

$sort = $request->has('sort') ? $request->query('sort') : 'desc';

if($sort == 'asc') {

    $statuses = $event->statuses()->oldest()->paginate(10);

} else {

    $statuses = $event->statuses()->latest()->paginate(10);

}

When an event has finished, it is marked as completed. Then the event is ordered by created_at desc. So that it reads from the beginning, like so:

if($sort == 'asc' || $event->completed) {

    $statuses = $event->statuses()->oldest()->paginate(10);

} else {

    $statuses = $event->statuses()->latest()->paginate(10);

}

However, if you then try and sort a completed event by statuses created_at DESC` it sees that the event is completed and orders by ASC.

if($sort == 'asc' || `$event->completed`) {

    $statuses = $event->statuses()->oldest()->paginate(10);

} else {

    $statuses = $event->statuses()->latest()->paginate(10);

}

How do I adjust it so that you can sort a completed event by statuses DESC?

Last updated 3 years ago.
0

Sign in to participate in this thread!

Eventy

Your banner here too?

chrisml chrisml Joined 14 Feb 2015

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2025 Laravel.io - All rights reserved.