Laravel-mongodb: Can't use paginate on raw aggregate lookup query

Created on 8 Aug 2018  路  6Comments  路  Source: jenssegers/laravel-mongodb

I cant use Paginate On my Code

I have this raw query

$dapil_kotkab = Dapil_Kotakab::raw(function($dapil_kotkab) { 
   return $dapil_kotkab->aggregate( [
                [ '$lookup' => [ 'as'=>'KecDetails', 'from'=>'src_kecamatan', 'foreignField'=>'id', 'localField'=>'idKecamatan' ]],
                [ '$lookup' => [ 'as'=>'KotDetails', 'from'=>'src_kota_kabupaten', 'foreignField'=>'code', 'localField'=>'idKota' ]],
                [ '$lookup' => [ 'as'=>'ProvDetails', 'from'=>'src_provinsi', 'foreignField'=>'idProv', 'localField'=>'idProvinsi' ]],
                ] );
        });

And i gonna to apply paginate on my code

$dapil_kotkab = Dapil_Kotakab::raw(function($dapil_kotkab) { 
            return $dapil_kotkab->aggregate( [
                [ '$lookup' => [ 'as'=>'KecDetails', 'from'=>'src_kecamatan', 'foreignField'=>'id', 'localField'=>'idKecamatan' ]],
                [ '$lookup' => [ 'as'=>'KotDetails', 'from'=>'src_kota_kabupaten', 'foreignField'=>'code', 'localField'=>'idKota' ]],
                [ '$lookup' => [ 'as'=>'ProvDetails', 'from'=>'src_provinsi', 'foreignField'=>'idProv', 'localField'=>'idProvinsi' ]],
                ] );
        })->paginate(10);

But I have Error

Error Massage "Method Illuminate\Database\Eloquent\Collection::paginate does not exist."

Is any other option to solve this problem ?

_Thanks_, Nuhun , Matur nuwun , Terimakasih

Most helpful comment

@Nurjaman @syamardy00

Hello! The raw method returns the Laravel collection, so the paginate method is not available in it. You can go around and by yourself recreate the standard pagination.

$page = \Illuminate\Pagination\Paginator::resolveCurrentPage();
$perPage = 20;

$total = Dapil_Kotakab::count();

$collection = Dapil_Kotakab::raw(function ($collection) use ($page, $perPage) {
    return $collection->aggregate([
        [
            '$lookup' => [
                'as' => 'KecDetails',
                'from' => 'src_kecamatan',
                'foreignField' => 'id',
                'localField' => 'idKecamatan'
            ]
        ],
        [
            '$lookup' => [
                'as' => 'KotDetails',
                'from' => 'src_kota_kabupaten',
                'foreignField' => 'code',
                'localField' => 'idKota'
            ]
        ],
        [
            '$lookup' => [
                'as' => 'ProvDetails',
                'from' => 'src_provinsi',
                'foreignField' => 'idProv',
                'localField' => 'idProvinsi'
            ]
        ],
        ['$skip' => ($page - 1) * $perPage],
        ['$limit' => $perPage],
    ]);
});

return new \Illuminate\Pagination\LengthAwarePaginator($collection, $total, $perPage, $page, [
    'path' => \Illuminate\Pagination\Paginator::resolveCurrentPath(),
]);

All 6 comments

I have a same problem too,
Hopefully to find a solution soon..

Haturnuhun sadayana.

@Nurjaman @syamardy00

Hello! The raw method returns the Laravel collection, so the paginate method is not available in it. You can go around and by yourself recreate the standard pagination.

$page = \Illuminate\Pagination\Paginator::resolveCurrentPage();
$perPage = 20;

$total = Dapil_Kotakab::count();

$collection = Dapil_Kotakab::raw(function ($collection) use ($page, $perPage) {
    return $collection->aggregate([
        [
            '$lookup' => [
                'as' => 'KecDetails',
                'from' => 'src_kecamatan',
                'foreignField' => 'id',
                'localField' => 'idKecamatan'
            ]
        ],
        [
            '$lookup' => [
                'as' => 'KotDetails',
                'from' => 'src_kota_kabupaten',
                'foreignField' => 'code',
                'localField' => 'idKota'
            ]
        ],
        [
            '$lookup' => [
                'as' => 'ProvDetails',
                'from' => 'src_provinsi',
                'foreignField' => 'idProv',
                'localField' => 'idProvinsi'
            ]
        ],
        ['$skip' => ($page - 1) * $perPage],
        ['$limit' => $perPage],
    ]);
});

return new \Illuminate\Pagination\LengthAwarePaginator($collection, $total, $perPage, $page, [
    'path' => \Illuminate\Pagination\Paginator::resolveCurrentPath(),
]);

@RomM1

Hi, Thank you very much, this is very helpful .. :)

@RomM1 Are you a hero? I don't solved since 2 days. But you become a hero to my problem. Thank you.

@RomM1 Are you a hero? I don't solved since 2 days. But you become a hero to my problem. Thank you.

i know its too late but you can use toQuery() method at the end of your raw()

Thank @matinkhosravani it works for me

$limit = 10;
$page = 1;
$abcResult = Abc::raw(function ($collection) {
      return $collection->aggregate([
          ['$sort' => ['updated_at' => -1]]
      ]);
  });
return $abcResult->toQuery()->paginate($limit, ['*'], 'page', $page);

----------------------------------- update ---------------
It does not work with

$abcResult = Abc::raw(function ($collection) {
    return $collection->aggregate([
        ['$addFields' => ['countLikes' => ['$size' => '$likes']]],
        ['$sort' => ['countLikes' => -1]]
    ]);
});

It not have new column countLike in result and sort order wrong although $abcResult->toQuery() has new column.

Was this page helpful?
0 / 5 - 0 ratings