Belio's avatar
Level 18

Returning only the items from the last page in pagination

Hello everyone.

This code bellow it is working fine, but now I want to return it in reverse. I mean, return only the items from the last page so I can set as active order the last one I stored.

public function store(Request $request)
{
        $order = new Order($request->all());
        Auth::user()->orders()->save($order);

        $orders = Order::with('lines', 'table')->whereCashed(false)->paginate(12);

        return $this->respondWithPagination($orders, [
            'data' => $this->orderTransformer->transformCollection($orders->all())
        ]);
    }
0 likes
2 replies
tisuchi's avatar

Try with -

 $orders = Order::with('lines', 'table')->whereCashed(false)->latest()->paginate(12);
Belio's avatar
Level 18

The thing is I have to keep the order. I did the trick with this. I don't know if there is some better solution. Thanks!

$limit = 12;
$orders = Order::with('lines', 'table')->whereCashed(false);

        $currentPage = ceil ($orders->count() / $limit);

        Paginator::currentPageResolver(function() use ($currentPage) {
            return $currentPage;
        });

        $orders = $orders->paginate($limit);

Please or to participate in this conversation.