如何使用LaravelAPI资源对加载的关系进行分页

如何使用LaravelAPI资源对加载的关系进行分页,laravel,api,pagination,resources,laravel-pagination,Laravel,Api,Pagination,Resources,Laravel Pagination,我需要在它的资源中加载模型关系并对它们进行分页 在我的例子中,我有类别和路径模型,还有类别资源和路径资源 CategoryResource的toArray方法如下: public function toArray($request) { return [ 'id' => $this->id, 'name' => $this->name, 'slug' => $this->slug, '

我需要在它的资源中加载模型关系并对它们进行分页

在我的例子中,我有
类别
路径
模型,还有
类别资源
路径资源

CategoryResource
toArray
方法如下:

public function toArray($request)
{
    return [
        'id'   => $this->id,
        'name' => $this->name,
        'slug' => $this->slug,
        'order' => $this->order,
        'paths' => PathResource::collection($this->whenLoaded('paths'))
    ];
}
public function toArray($request)
{
    return parent::toArray($request);
}
toArray
PathResource
方法如下:

public function toArray($request)
{
    return [
        'id'   => $this->id,
        'name' => $this->name,
        'slug' => $this->slug,
        'order' => $this->order,
        'paths' => PathResource::collection($this->whenLoaded('paths'))
    ];
}
public function toArray($request)
{
    return parent::toArray($request);
}

问题是如何在我的
类别资源中加载和分页相关的
路径

您可能应该查看有关资源和资源集合的文档。ResourceCollections将允许您轻松地对资源进行分页

我遇到了同样的问题,并通过以下方式解决:

先决条件

您必须拥有/创建
Path
model的资源,即
PathResource
要创建一个,请使用以下命令:

php artisan make:resource PathResource

解决方案

解决方案是对关系使用laravel
paginate
,并对已分页的集合使用
transform
方法将其项转换为资源

第一步

使用以下命令创建用于对应用程序中的任何资源进行分页的基类:

php artisan make:resource PaginatedCollection-c

编辑分页集合
并添加以下代码:

namespace App\Http\Resources;

use Illuminate\Http\Resources\Json\ResourceCollection;

class PaginatedCollection extends ResourceCollection
{

    /**
     * An array to store pagination data that comes from paginate() method.
     * @var array
     */
    protected $pagination;

    /**
     * PaginatedCollection constructor.
     *
     * @param mixed $resource paginated resource using paginate method on models or relations.
     */
    public function __construct($resource)
    {

        $this->pagination = [
            'total' => $resource->total(), // all models count
            'count' => $resource->count(), // paginated result count
            'per_page' => $resource->perPage(),
            'current_page' => $resource->currentPage(),
            'total_pages' => $resource->lastPage()
        ];

        $resource = $resource->getCollection();

        parent::__construct($resource);
    }

    /**
     * Transform the resource collection into an array.
     * now we have data and pagination info.
     *
     * @param \Illuminate\Http\Request $request
     * @return array
     */
    public function toArray($request)
    {
        return [
            // our resources
            'data' => $this->collection,

            // pagination data
            'pagination' => $this->pagination
        ];
    }
}
第二步

为您的模型创建一个集合资源,并扩展
paginedCollection
而不是默认的
ResourceCollection

运行此命令执行此操作:
php工匠制作:资源路径集合-c

现在编辑新的集合类
PathCollection
并重写
toArray
方法:

/**
 * Transform the resource collection into an array.
 *
 * In this method use your already created resources
 * to avoid code duplications
 *
 * @param \Illuminate\Http\Request $request
 * @return array
 */
public function toArray($request)
{
    return [

        // Here we transform any item in paginated items to a resource

        'data' => $this->collection->transform(function ($path) {
            return new PathResource($path);
        }),

        'pagination' => $this->pagination,
    ];
}
最后一步

在您的
类别资源中
使用
路径集合
如下:

return [
    'id'   => $this->id,
    'name' => $this->name,
    'slug' => $this->slug,
    'order' => $this->order,
    'paths' => new PathCollection(
        new LengthAwarePaginator(
            $this->whenLoaded('paths'),
            $this->paths_count,
            10
        )
    ),
];
并确保导入
LengthAwarePaginator
class:
使用light\Pagination\LengthAwarePaginator

用法

$category = Category::with('paths')->withCount('paths')->find(1);
return new CategoryResource($category);

你找到解决这个问题的办法了吗?我也被同一个问题深深打动,找不到答案answer@LizeshShakya我发布了这个问题的答案。文档只显示了如何对整个模型进行分页。问题是如何在toArray方法中对资源中的模型关系进行分页?