Ad
Laravel Eloquent Pagination, How To Count Before Pagination With Filters?
Here my code :
$prestations = Prestation::with('service','facility','conciergeries.network')
->whereHas('service', function ($query) use ($searchService) {
$query->select('id','name')->where('name', 'regexp', "/$searchService/i");
})
->whereHas('facility', function ($query) use ($searchPartenaire) {
$query->select('id','name')->where('name', 'regexp', "/$searchPartenaire/i");
})
->whereHas('conciergeries.network', function ($query) use ($searchFiliale) {
$query->select('id','name')->where('name', 'regexp', "/$searchFiliale/i");
})
->where('name', 'regexp', "/$search/i")
->orderBy($orderBy, $orderDirection)
->simplePaginate(50);
$res = [
'results' => $prestations,
'total' => Prestation::all()->count(),
];
return $res;
Hi, actually i get all "Prestation" items at 'total', but i need the real total just before the simplePaginate(50);
after all of my conditions.
How i can give this value at 'total' without disturbing my code ?
Thank you !
Ad
Answer
Eloquent queries are very flexible and you can store them as variables before executing them.
Here is a simplified example of what you could do:
// Start your query and store it as a variable
$query = Prestation::where('name', 'regexp', "/$search/i");
// Now count the total number of entries which will return
$count = $query->count();
// Finally, do your pagination
$prestations = $query->simplePaginate(50);
Of course you can add all of your other constraints to the query, i've just removed them to simplify the answer.
Ad
source: stackoverflow.com
Related Questions
- → "failed to open stream" error when executing "migrate:make"
- → October CMS Plugin Routes.php not registering
- → OctoberCMS Migrate Table
- → OctoberCMS Rain User plugin not working or redirecting
- → October CMS Custom Mail Layout
- → October CMS - How to correctly route
- → October CMS create a multi select Form field
- → October CMS - Conditionally Load a Different Page
- → How to disable assets combining on development in OctoberCMS
- → October CMS - Radio Button Ajax Click Twice in a Row Causes Content to disappear
- → OctoberCms component: How to display all ID(items) instead of sorting only one ID?
- → In OctoberCMS how do you find the hint path?
- → How to register middlewares in OctoberCMS plugin?
Ad