为什么我可以在雄辩的代码中使用最新()?

发布于 01-21 16:19 字数 258 浏览 3 评论 0原文

我一直在错误 “方法照亮\ 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

我不是你的备胎 2025-01-28 16:19:35

最新()是一个建筑商类方法不是收集类方法,请参见: https://laravel.com/api/8.x/illuminate/database/eloquent/eloquent/builder.html#method_latestest

因此,请在获得code>之前使用它get()paginate()

$items=Item::select('*')
    ->orderBy('version','DESC')
    ->latest()
    ->get()
    ->unique('name');

使用分页上的唯一值更为复杂。如果有更多的记录,我们需要知道什么要选择相同名称。最简单的方法是使用DISTINT(),但这可能不是您需要的:

$items=Item::select('*')
    ->orderBy('version','DESC')
    ->latest()
    ->distinct()
    ->paginate(5);

其他可能性是使用groupby('name')而不是不同(),但是您必须仔细汇总“名称”(或仅选择“名称”列)的其他列。示例:

$items=Item::select('name', DB::raw('MAX(version) as max_version'))
    ->orderBy('max_version','DESC')    
    ->group_by('name')
    ->paginate(5);

如果您需要更多列,则使用适当的汇总函数max()或first(),last(),avg()等

来获取整个行(无法使用simple'*'):

$max_version_table = Item::select('model_name', DB::raw('MAX(version) as max_version'))->groupBy('model_name');

$items = Item::select('*')->joinSub($max_version_table, 'max_version_table', function($join){
  $join->on('items.model_name','=','max_version_table.model_name');
  $join->on('items.version','=','max_version_table.max_version');
})->paginate(5);

latest() is a Builder class method not a Collection class method see: https://laravel.com/api/8.x/Illuminate/Database/Eloquent/Builder.html#method_latest

So use it before getting the collection by get() or paginate():

$items=Item::select('*')
    ->orderBy('version','DESC')
    ->latest()
    ->get()
    ->unique('name');

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:

$items=Item::select('*')
    ->orderBy('version','DESC')
    ->latest()
    ->distinct()
    ->paginate(5);

Other possibility is to use groupBy('name') instead of distinct(), but there you must carefully aggregate the other columns around the 'name' (or select only the 'name' column). Example:

$items=Item::select('name', DB::raw('MAX(version) as max_version'))
    ->orderBy('max_version','DESC')    
    ->group_by('name')
    ->paginate(5);

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):

$max_version_table = Item::select('model_name', DB::raw('MAX(version) as max_version'))->groupBy('model_name');

$items = Item::select('*')->joinSub($max_version_table, 'max_version_table', function($join){
  $join->on('items.model_name','=','max_version_table.model_name');
  $join->on('items.version','=','max_version_table.max_version');
})->paginate(5);

恏ㄋ傷疤忘ㄋ疼 2025-01-28 16:19:35

Laravel's 最新方法在 Collection 上不起作用,它在查询构建器上起作用。因此,首先,使用最新方法,然后使用 paginate

$items = Item::select('*')
     ->orderBy('version','DESC')
     ->latest()
     ->distinct()
     ->paginate(5);

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.

$items = Item::select('*')
     ->orderBy('version','DESC')
     ->latest()
     ->distinct()
     ->paginate(5);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文