#1 Collection or Array custom paging

namespace App\Http\Traits;

use Illuminate\Pagination\LengthAwarePaginator;
use Illuminate\Pagination\Paginator;
use Illuminate\Support\Collection;

trait CollectionPaginate {
    /** * collect or array page **@param array|Collection      $items
     * @param int   $perPage
     * @param int  $page
     * @param array $options
     *
     * @return LengthAwarePaginator
     */
    public function paginate($items.$perPage = 15.$page = null.$options = [])
    {
        $page = $page? : (Paginator::resolveCurrentPage() ? :1);

        if(! array_key_exists('path'.$options)) {
            $options['path'] = Paginator::resolveCurrentPath();
        }

        $items = $items instanceof Collection ? $items : Collection::make($items);

        return new LengthAwarePaginator($items->forPage($page.$perPage), $items->count(), $perPage.$page.$options); }}Copy the code
use CollectionPaginate;
/ /... Other code
// Generate pages
$hotels = $this->paginate($hotels.10);
// Keep the parameters when turning the page
$hotels->appends(['city_id'= >$city_id.'city_name'= >$city_name.'date_range'= >$date_range]);
/ /... Other code
Copy the code