0

I use the standart method for pagination:

$ann = Ann::get()->orderBy('id', 'desc')->paginate($limit);

After I do merge $ann with another collection:

$ann = $ann->merge($ann_subscribed);
            $ann = $ann->all();

In result I get $ann = $ann->all(); without pagination

Darama
  • 3,130
  • 7
  • 25
  • 34

1 Answers1

1

1- add this to boot function in \app\Providers\AppServiceProvider

 /**
         * Paginate a standard Laravel Collection.
         *
         * @param int $perPage
         * @param int $total
         * @param int $page
         * @param string $pageName
         * @return array
         */
        Collection::macro('paginate', function($perPage, $total = null, $page = null, $pageName = 'page') {
            $page = $page ?: LengthAwarePaginator::resolveCurrentPage($pageName);
            return new LengthAwarePaginator(
                $this->forPage($page, $perPage),
                $total ?: $this->count(),
                $perPage,
                $page,
                [
                    'path' => LengthAwarePaginator::resolveCurrentPath(),
                    'pageName' => $pageName,
                ]
            );
        });

2-From hereafter for all collection you can paginate like this

$ann = Ann::orderByRaw('id','DESC')->get();
$ann_merged = $ann->merge($ann_subscribed);
$ann_merged ->paginate(5);
Rohallah Hatami
  • 525
  • 6
  • 12