Skip to content

Instantly share code, notes, and snippets.

Gustavo Straube straube

View GitHub Profile
@straube
straube / pagination.php
Last active Oct 12, 2016
Laravel 5 pagination with grouping functions
View pagination.php
// Build your query as usual, but don't use the paginate() method at the chain
// end.
$query = User::select([ 'users.*', DB::raw('count(roles.id) as roles_count') ])
->join('user_roles', 'users.id', '=', 'roles.user_id')
->groupBy('users.id')
->having('roles_count', '>', 0);
// Due a bug in Eloquent, we need to build the paginator manually.
//
// For more details refer to:
You can’t perform that action at this time.