【问题标题】:Laravel, use pagination Method Illuminate\Database\Eloquent\Collection::render does not existLaravel,使用分页方法 Illuminate\Database\Eloquent\Collection::render 不存在
【发布时间】:2020-03-22 01:58:17
【问题描述】:

我正在尝试使用 paginate 但它不起作用,没有 paginate 一切都很好但是当我使用它时出现此错误 方法 Illuminate\Database\Eloquent\Collection::links 不存在

控制器:

$userorders=Userorder::where('user_id', $user_id)
    ->with('storeinfo')
    ->with('product')
    ->paginate(15)
    ->groupBy('order_number');

查看:

@if( count($userorders) > 0 )
    {!! $userorders->links() !!}
@endif 

【问题讨论】:

  • ->groupBy() 返回一个Collection,并且集合没有links() 方法。 (或标题所暗示的 render() 方法。)如果您切换顺序,它可能会起作用,[...]->groupBy(...)->paginate();
  • 检查这个我认为这是同样的问题stackoverflow.com/a/56278462/10066194
  • @TimLewis 你能解释一下吗,你的意思是我必须把分页放在 groupby 之后?
  • @TimLewis 如果我这样做,那么我会在刀片中收到此错误:在布尔值上调用成员函数 product()
  • 就像我说的,它可能起作用。但这是一个不同的错误。是什么代码产生的?

标签: php laravel


【解决方案1】:

因为links()LengthAwarePaginator 的一个方法,而不是groupBy 将返回一个Collection,所以你可以让你的UserOrder 按其order_number 分组的唯一方法是将调用的顺序切换到这个一个

$builder= Userorder::where('user_id',$user_id)->with('storeinfo')->with('product');
$userOrdersPagination=$builder->paginate(15);
$userorders=$userOrdersPagination->groupBy('order_number')

然后您还必须传递给视图 $userOrdersPagination 并使用它来生成链接

【讨论】:

  • 刀片内部我使用另一个forloop,如果我删除它,它会正常工作,但我需要它:@foreach($groupOrders as $key=>$userorder) {{$userorder->product() ->first()->product_name}},@endforeach
  • 好吧,在这种情况下,你不能在一行代码中完成所有事情,试试问题上的代码,我现在就编辑
  • 看不到?现在代码应该是 3 行,并且在它下面还有一个新的文本行
猜你喜欢
  • 2021-03-26
  • 1970-01-01
  • 2021-05-30
  • 2020-03-25
  • 1970-01-01
  • 2021-08-02
  • 1970-01-01
  • 1970-01-01
  • 2020-10-25
相关资源
最近更新 更多