英文:
Laravel Eloquent Pagination, how to count before pagination with filters?
问题
你好,你可以尝试在 $prestations
查询链中添加 count
方法来获取在应用分页之前的总数,然后将该总数分配给 'total'
键。以下是修改后的代码:
$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);
$total = $prestations->count(); // 获取总数
$prestations = $prestations->simplePaginate(50);
$res = [
'results' => $prestations,
'total' => $total, // 分配总数
];
return $res;
这样,你可以在应用分页之前获取并分配总数,而不会影响你的代码逻辑。
英文:
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 !
答案1
得分: 3
Eloquent查询非常灵活,你可以在执行之前将它们存储为变量。
以下是一个简化的示例:
// 开始查询并将其存储为变量
$query = Prestation::where('name', 'regexp', "/$search/i");
// 现在计算总条目数
$count = $query->count();
// 最后,进行分页
$prestations = $query->simplePaginate(50);
当然,你可以在查询中添加所有其他约束条件,我只是为了简化回答而将它们移除了。
英文:
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.
答案2
得分: 1
请尝试以下内容:
$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);
$res = [
'results' => $prestations->simplePaginate(50),
'total' => $prestations->count(),
];
return $res;
如果您需要进一步的帮助,请告诉我。
英文:
Please try this
$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);
$res = [
'results' => $prestations->simplePaginate(50),
'total' => $prestations->count(),
];
return $res;
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论