Ad

Laravel Append Overall Total Value On Pagination Result

- 1 answer

I have a Laravel query using pagination.

I want to be able to return the result based on the pagination but also get the overall total of the query and append this to the return. So for example the pagination is set to 5 but the overall total might be 20.

$query = Model::paginate(5); 
$queryTotal = $query->total();
$query->append($queryTotal);
return $query;
Ad

Answer

The Laravel Paginator does this already.

You can see that when serializing results to JSON there is a total key which represents all rows matching that query.

You can also see there is a total method available from the paginator:

$results->total()

Along side other methods that can be found in the Pagination Docs

Ad
source: stackoverflow.com
Ad