Home

laravel 分页返回信息简化

重写分页返回数组LengthAwarePaginatorService

<?php
namespace App\Providers;
/**
 * 重写分页返回数组
 * Class LengthAwarePaginatorService
 * @package App\Providers
 */
class LengthAwarePaginatorService extends \Illuminate\Pagination\LengthAwarePaginator
{
    public function toArray()
    {
        return [
            'data' => $this->items->toArray(),
            'total' => $this->total(),
            'current_page' => $this->currentPage(),
            'per_page' => intval($this->perPage()),
            'last_page' => $this->lastPage()
        ];
    }
}

AppServiceProvider绑定服务

<?php

namespace App\Providers;

use Illuminate\Support\ServiceProvider;
use Illuminate\Support\Facades\Schema;

class AppServiceProvider extends ServiceProvider
{
    /**
     * Register any application services.
     *
     * @return void
     */
    public function register()
    {
        $this->app->bind('Illuminate\Pagination\LengthAwarePaginator',function ($app,$options){
            return new LengthAwarePaginatorService($options['items'], $options['total'], $options['perPage'], $options['currentPage'] , $options['options']);
        });
    }

    /**
     * Bootstrap any application services.
     *
     * @return void
     */
    public function boot()
    {
        Schema::defaultStringLength(191);
    }
}