简体   繁体   English

是否可以在刀片视图中使用groupBy在laravel中进行分页?

[英]Is it possible to do pagination in laravel with groupBy in blade view?

I'm fetching record for multiple section in a view. 我正在获取视图中多个部分的记录。 To show complete data I am simply doing $data = Data::all(); 为了显示完整的数据,我只是在做$data = Data::all();

Then simply getting counts for each section as required. 然后只需根据需要获取每个部分的计数。 For example: 例如:

{{count($data->where('status', 'HOLD'))}}

For another section on the same view I want to show result with Groupy For Example: 对于同一视图的另一部分,我想使用Groupy显示结果,例如:

@foreach($data->paginate()->groupBy('user_id') as $byUser)
@endforeach

Then show the pagination {{$data->links()}} 然后显示分页{{$data->links()}}

I have already tried by separating the result in the controller and Calling the data using different methods. 我已经尝试过通过在控制器中分离结果并使用不同的方法调用数据来进行尝试。

$data = Data::all();
$userData = Data:paginate(5)->groupBy('user_id');
return view('data.all', compact('data', 'userData'));

This is working well without pagination. 在没有分页的情况下运行良好。 After adding pagination I am getting below error. 添加分页后,我得到以下错误。

Method Illuminate\Database\Eloquent\Collection::paginate does not exist. (View: C:\xampp\htdocs\project\resources\views\data\all.blade.php)

Is there any way to work it out? 有什么办法解决吗? Thanks. 谢谢。

Check this line: 检查这一行:

$data = Data::all();

Here you are already executing the query, this means, a Collection instance is returned. 在这里您已经在执行查询,这意味着将返回Collection实例。 That's why you can't use the paginate() method, because that is a QueryBuilder method. 这就是为什么您不能使用paginate()方法的原因,因为那是QueryBuilder方法。

Try this: 尝试这个:

$userData = Data::groupBy('user_id')->paginate(5);

return view('data.all', compact('data', 'userData'));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM