简体   繁体   中英

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();

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:

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

Then show the pagination {{$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. That's why you can't use the paginate() method, because that is a QueryBuilder method.

Try this:

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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