gpt4 book ai didi

laravel - 如何使用 Laravel API 资源对加载的关系进行分页

转载 作者:行者123 更新时间:2023-12-03 21:01:34 25 4
gpt4 key购买 nike

我需要在它的资源中加载模型关系并对它们进行分页。
就我而言,我有 CategoryPath型号,加上 CategoryResourcePathResourcetoArray CategoryResource的方法如下所示:

public function toArray($request)
{
return [
'id' => $this->id,
'name' => $this->name,
'slug' => $this->slug,
'order' => $this->order,
'paths' => PathResource::collection($this->whenLoaded('paths'))
];
}
toArray PathResource的方法如下所示:
public function toArray($request)
{
return parent::toArray($request);
}
问题 是如何加载和分页相关的 Path在我的 CategoryResource ?

最佳答案

我遇到了同样的问题并以这种方式解决了它:
先决条件
您必须为 Path 拥有/创建资源模型即 PathResource创建一个使用这个命令:php artisan make:resource PathResource 解决方案
解决方法是使用laravel paginate关于关系和使用 transform分页集合上的方法将其项目转换为您的资源。
第一步
使用以下命令创建用于对应用程序中的任何资源进行分页的基类:php artisan make:resource PaginatedCollection -c编辑 PaginatedCollection并添加以下代码:

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
];
}
}
第二步
为您的模型制作集合资源并扩展 PaginatedCollection而不是默认 ResourceCollection .
运行此命令以执行此操作: php artisan make:resource PathCollection -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,
];
}
最后一步
在您的 CategoryResource使用 PathCollection像这样:
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): use Illuminate\Pagination\LengthAwarePaginator; 用法
$category = Category::with('paths')->withCount('paths')->find(1);
return new CategoryResource($category);

关于laravel - 如何使用 Laravel API 资源对加载的关系进行分页,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57151461/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com