Laravel: How to get last N entries from DB

前端 未结 7 855
太阳男子
太阳男子 2021-02-01 01:07

I have table of dogs in my DB and I want to retrieve N latest added dogs.

Only way that I found is something like this:

Do         


        
7条回答
  •  粉色の甜心
    2021-02-01 01:13

    You can pass a negative integer n to take the last n elements.

    Dogs::all()->take(-5)
    

    This is good because you don't use orderBy which is bad when you have a big table.

提交回复
热议问题