为什么我可以在雄辩的代码中使用最新()?
我一直在错误 “方法照亮\ database \ eloquent \ collection ::最新的不存在”,在我的代码的这一行上,
$items=Item::select('*')
->orderBy('version','DESC')
->get()
->unique('name')->latest()->paginate(5);
我不确定为什么以及如何更改代码?
I have been getting an error
'Method Illuminate\Database\Eloquent\Collection::latest does not exist' on this line of my code
$items=Item::select('*')
->orderBy('version','DESC')
->get()
->unique('name')->latest()->paginate(5);
I'm not really sure why and how should I change my code?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
最新()
是一个建筑商类方法不是收集类方法,请参见: https://laravel.com/api/8.x/illuminate/database/eloquent/eloquent/builder.html#method_latestest因此,请在获得
code>之前使用它get()
或paginate()
:使用分页上的唯一值更为复杂。如果有更多的记录,我们需要知道什么要选择相同名称。最简单的方法是使用
DISTINT()
,但这可能不是您需要的:其他可能性是使用
groupby('name')
而不是不同()
,但是您必须仔细汇总“名称”(或仅选择“名称”列)的其他列。示例:如果您需要更多列,则使用适当的汇总函数max()或first(),last(),avg()等
来获取整个行(无法使用simple'*'):
latest()
is a Builder class method not a Collection class method see: https://laravel.com/api/8.x/Illuminate/Database/Eloquent/Builder.html#method_latestSo use it before getting the collection by
get()
orpaginate()
:Using pagination on unique values is a bit more complicated. We would need to know what to select if there is more records with the same name. The most simple approach is to use
distinct()
but this may not be what you need:Other possibility is to use
groupBy('name')
instead ofdistinct()
, but there you must carefully aggregate the other columns around the 'name' (or select only the 'name' column). Example:If you need more columns selected use appropriate aggregate functions MAX() or FIRST(), LAST(), AVG(), etc..
To get the whole row (simple '*' cannot be used):
Laravel's 最新方法在 Collection 上不起作用,它在查询构建器上起作用。因此,首先,使用最新方法,然后使用 paginate 。
Laravel's latest method doesn't work on the collection, it works on Query-Builder. So, first, use the latest method and then use paginate.