在Laravel分页之前获取所有结果ID

时间:2020-01-22 14:44:38

标签: php laravel eloquent pagination

这是我的代码:

$prestations = Prestation::with([
        'service' => function($query) {
            $query->select(['id','name']);
        },
        'facility' => function($query) {
            $query->select(['id','name']);
        },
        'conciergeries.network' => function($query) {
            $query->select(['id','name']);
        }
    ])
    ->whereHas('service', function ($query) use ($sService) {
        $query->where('name', 'regexp', "/$sService/i");
    })
    ->whereHas('facility', function ($query) use ($sPartner) {
        $query->where('name', 'regexp', "/$sPartner/i");
    })
    ->whereHas('conciergeries.network', function ($query) use ($sSubsidiary) {
        $query->where('name', 'regexp', "/$sSubsidiary/i");
    })
    ->options([
        'collation' => [
            'locale' => 'en_US',
            'strength' => 1
        ]
    ])
    ->where('name', 'regexp', "/$search/i")
    ->orderBy($orderBy, $orderDirection)
    ->paginate(25);

当我尝试获取所有ID时:

$arrIds = [];

foreach ($prestations as $prestation) {
    array_push($arrIds, $prestation->_id);
}

问题是我只能获得25个分页元素的ID。 如何获取所有结果的ID?

1 个答案:

答案 0 :(得分:1)

您可以将查询保留在$prestationsQuery变量上,并调用两次,如下所示:

$prestationsQuery = Prestation::with([
        'service:id,name',
        'facility:id,name',
        'conciergeries.network' => function($query) {
            $query->select(['id','name']);
        }
    ])
    ->whereHas('service', function ($query) use ($sService) {
        $query->where('name', 'regexp', "/$sService/i");
    })
    ->whereHas('facility', function ($query) use ($sPartner) {
        $query->where('name', 'regexp', "/$sPartner/i");
    })
    ->whereHas('conciergeries.network', function ($query) use ($sSubsidiary) {
        $query->where('name', 'regexp', "/$sSubsidiary/i");
    })
    ->options([
        'collation' => [
            'locale' => 'en_US',
            'strength' => 1
        ]
    ])
    ->where('name', 'regexp', "/$search/i")
    ->orderBy($orderBy, $orderDirection);

$arrIds = $prestationsQuery->pluck('_id');

$prestations = $prestationsQuery->paginate(25);

请记住在paginate之后致电pluck

另一种方法是使用tap函数,如下所示:

$arrIds = [];

$prestations = Prestation::with([
        'service:id,name',
        'facility:id,name',
        'conciergeries.network' => function($query) {
            $query->select(['id','name']);
        }
    ])
    ->whereHas('service', function ($query) use ($sService) {
        $query->where('name', 'regexp', "/$sService/i");
    })
    ->whereHas('facility', function ($query) use ($sPartner) {
        $query->where('name', 'regexp', "/$sPartner/i");
    })
    ->whereHas('conciergeries.network', function ($query) use ($sSubsidiary) {
        $query->where('name', 'regexp', "/$sSubsidiary/i");
    })
    ->options([
        'collation' => [
            'locale' => 'en_US',
            'strength' => 1
        ]
    ])
    ->where('name', 'regexp', "/$search/i")
    ->orderBy($orderBy, $orderDirection);
    ->tap(function ($query) use (&$arrIds) {
        $arrIds = $query->pluck('_id');
    })
    ->paginate(25);