strangequirks
2 weeks ago
132
2
General

How to get the latest item by id.

Posted 2 weeks ago by strangequirks

I am trying to do get all the latest items and sort by id descending (i.e. get all items that were just added with a limit and offset).

This is my code-

$products = Product::all()
                ->slice($request->get('offset'))
                ->take($request->get('limit'))
                ->sortByDesc('id')
                ->toBase();

However it seems when I have more that that limit, then I dont have the right order. It gets me say 10 products but not sorted corrected. Any idea how to do this?

Please sign in or create an account to participate in this conversation.