Laravel Eloquent Pagination, how to count before pagination with filters?

huangapple go评论139阅读模式
英文:

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;

huangapple
  • 本文由 发表于 2020年1月3日 22:11:21
  • 转载请务必保留本文链接:https://go.coder-hub.com/59580018.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定