Categories
Laravel

Laravel Get Latest Record

The code above is a repository method to get the last product created. This is useful if you want to display the most recently created product on your website, for example.

The code uses the Laravel query builder to order the products by id in descending order and then gets the first product from the resulting collection. You could also order the products by created_at in descending order, or use the latest() method to accomplish the same thing.

Leave a Reply

Your email address will not be published.