简体   繁体   中英

Custom/Extend laravel pagination method

I use vue component for displaying data on my blade template layouts, like for example pagination data, that's why I created a method to return paginated data in the shape I need it, I was wondering if it's posible to chain or override laravel paginate method? Right now I'm doing it like this but I'd like to maybe extend laravel query builder or whatever classes laravel uses to paginate:

$posts = Post::latest()->with(['category','user'])->paginate($request->input('paginate', 6));

$posts = $this->getPagination($posts);

public function getPagination($results)
    {
        return 
        [
            'data' => $results,
            'pagination' => [
                'total' => $results->total(),
                'per_page' =>$results->perPage(),
                'current_page' => $results->currentPage(),
                'last_page' => $results->lastPage(),
                'from' => $results->firstItem(),
                'to' => $results->lastItem()
            ]
        ];    
    }


I'd like to have something like:

$posts = Post::paginate($request->input('paginate', 6))->getPaginatedData();

When building an API, you may need a transformation layer that sits between your Eloquent models and the JSON responses that are actually returned to your application's users. Laravel's resource classes allow you to expressively and easily transform your models and model collections into JSON.

Then you can do something like this...

<?php

namespace App\Http\Resources;

use Illuminate\Http\Resources\Json\JsonResource;

class Post extends JsonResource
{
    /**
     * Transform the resource into an array.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Illuminate\Pagination\Paginator $result
     * @return array
     */
    public function toArray($request, $result)
    {
        return [
            'data' => $results,
            'pagination' => [
                'total' => $results->total(),
                'per_page' =>$results->perPage(),
                'current_page' => $results->currentPage(),
                'last_page' => $results->lastPage(),
                'from' => $results->firstItem(),
                'to' => $results->lastItem()
        ];
    }
}

I place Post by only one name according to your question. But you can do something like Paginate instead of Post. And pass all the models through this api resource.

For example:

class Paginate extends JsonResource
{
    /**
     * Transform the resource into an array.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Illuminate\Pagination\Paginator $result
     * @return array
     */
    public function toArray($request, $result)
    {
        return [
            'data' => $results,
            'pagination' => [
                'total' => $results->total(),
                'per_page' =>$results->perPage(),
                'current_page' => $results->currentPage(),
                'last_page' => $results->lastPage(),
                'from' => $results->firstItem(),
                'to' => $results->lastItem()
        ];
    }
}

Then...

use App\Http\Resources\Paginate as PaginateResource;
use App\User;

Route::get('/user', function () {
    return new PaginateResource(User::paginate());
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM