gpt4 book ai didi

mysql - 如何使用 Eloquent 获取 N 个最新的数据库条目?

转载 作者:行者123 更新时间:2023-11-30 01:23:08 24 4
gpt4 key购买 nike

我尝试获取数据库中的最新帖子。我试过这个:

$users = Post::take(10)->get();

这会生成该模型中的前 10 个帖子。我想要最新的10张。我有表行 created_atupdated_at

最佳答案

作为 Paolo Bergantino said ,这将完成这项工作:

# Get n latest posts #
$n = 10;
$latest_posts = Post::orderBy('created_at', 'DESC')->take($n)->get();

有关更多信息,请查看文档部分:Select - Order By, Group By, And Having .

关于mysql - 如何使用 Eloquent 获取 N 个最新的数据库条目?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18316663/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com