Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
245 views
in Technique[技术] by (71.8m points)

Laravel 7 - Pagination on Collection

I need to get my data with pagination when I use collection.

Couldn't find any way, and nothing works that written on documents.

Here's my controller;

...
$data = $process->paginate(30);

$data = OrderResource::collection($data);

And here's my resource:

<?php

namespace AppHttpResources;
use CarbonCarbon;
use IlluminateSupportFacadesAuth;

use IlluminateHttpResourcesJsonJsonResource;

class OrderResource extends JsonResource
{
    /**
     * Transform the resource into an array.
     *
     * @param  IlluminateHttpRequest  $request
     * @return array
     */
    public function toArray($request)
    {
        $user = Auth::user();
        return [
            "id" => $this->id,
            "customer" => $this->customer,
            "vehicle" => $this->vehicle,
            "basket" => $this->basket,
            "total" => money_formatter($this->total),
            "discount" => $this->discount,
            "net_total" => money_formatter($this->net_total),
            "status" => $this->status,
            "payment_type" => $this->payment_type,
            "main_name" => $this->vehicle->fleet_id ? $this->vehicle->fleet->title : ($this->customer->company_id ? $this->customer->company->title : $this->customer->fullname),
            "sub_name" => $this->vehicle->fleet_id ? ($this->customer->company_id ? $this->customer->company->title : $this->customer->fullname) : '',
            "created_at" => Carbon::parse($this->created_at)->formatLocalized('%a, %d %B %Y'),
        ];
    }
}
question from:https://stackoverflow.com/questions/65643641/laravel-7-pagination-on-collection

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You can add a macro inside your AppServiceProvider.php for this, inside the boot method.

       /**
         * Paginate a standard Laravel Collection.
         *
         * @param int $perPage
         * @param int $total
         * @param int $page
         * @param string $pageName
         * @return array
         */
        Collection::macro('paginate', function ($perPage = 15, $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,
                ]
            );
        });

And then you can use it like this (assuming $data is a regular laravel collection)

$data = $data->paginate(50);
dd($data);

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...