本文介紹了Laravel:如何從數據庫中獲取最后 N 個條目的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!
問題描述
我的數據庫中有狗表,我想檢索 N 個最新添加的狗
.
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:
Dogs:all()->where(time, <=, another_time);
還有其他方法嗎?例如像這樣的 Dogs:latest(5);
Is there another way how to do it? For example something like this Dogs:latest(5);
非常感謝您的幫助:)
推薦答案
你可以試試這樣的:
$dogs = Dogs::orderBy('id', 'desc')->take(5)->get();
使用 orderBy
和 Descending
順序并取前 n
條記錄.
Use orderBy
with Descending
order and take the first n
numbers of records.
更新(因為添加了latest
方法):
Update (Since the latest
method has been added):
$dogs = Dogs::latest()->take(5)->get();
這篇關于Laravel:如何從數據庫中獲取最后 N 個條目的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!
【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!