Bel*_*Bel 3 php laravel eloquent laravel-6 laravel-7
有人可以解释 ResourceCollection 和 JsonResource 之间的区别吗?
在 Laravel 6 文档中,您可以生成 2 种不同类型的资源……ResourceCollection 和 JsonResource。https://laravel.com/docs/6.x/eloquent-resources#resource-responses
<?php
namespace App\Http\Resources;
use Illuminate\Http\Resources\Json\ResourceCollection;
class ShopCollection extends ResourceCollection
{
/**
* Transform the resource collection into an array.
*
* @param \Illuminate\Http\Request $request
* @return array
*/
public function toArray($request)
{
return parent::toArray($request);
}
}
Run Code Online (Sandbox Code Playgroud)
对...
<?php
namespace App\Http\Resources;
use Illuminate\Http\Resources\Json\JsonResource;
class Shop extends JsonResource
{
/**
* Transform the resource into an array.
*
* @param \Illuminate\Http\Request $request
* @return array
*/
public function toArray($request)
{
return parent::toArray($request);
}
}
Run Code Online (Sandbox Code Playgroud)
ResourceCollection
用于项目列表,但JsonResource
用于单个项目。
我复制了在我的项目中使用它的一段代码:在我的项目中,我有文章列表,所以我必须显示文章列表,所以我使用ResourceCollection
它返回一系列文章:
<?php
namespace App\Http\Resources\Api\Collection;
use Illuminate\Http\Resources\Json\ResourceCollection;
class AlbumCollection extends ResourceCollection
{
/**
* Transform the resource collection into an array.
*
* @param \Illuminate\Http\Request $request
* @return array
*/
public function toArray($request)
{
return $this->collection->map(function ($item) {
return [
'id' => $item->id,
'name' => $item->name,
'description' => $item->description
];
});
}
public function with($request)
{
return [
'status' => true
];
}
}
Run Code Online (Sandbox Code Playgroud)
此代码返回给我一个文章列表。然后当用户点击一篇文章时,我必须向用户显示一篇文章,所以我必须返回一篇文章,我使用了以下代码:
<?php
namespace App\Http\Resources\Api\Resources;
use Illuminate\Http\Resources\Json\JsonResource;
class NewsResource extends JsonResource
{
/**
* Transform the resource into an array.
*
* @param \Illuminate\Http\Request $request
* @return array
*/
public function toArray($request)
{
return [
'id' => $this->id,
'title' => $this->title,
'body' => $this->body,
'cover' => $this->cover,
'view_count' => $this->view_count,
'comment_count' => $this->comment_count,
'like_count' => $this->like_count,
'created_at' => $this->created_at,
'updated_at' => $this->updated_at,
'comments' => $this->comments
];
}
public function with($request)
{
return [
'status' => true
];
}
}
Run Code Online (Sandbox Code Playgroud)
您可以看到与您的问题相关的我的回答
希望对你有帮助。
COLLECTION 用于多个资源。
我习惯这样做,例如:
客户端实体集合:
public function toArray($request)
{
return [
'data' => $this->collection->map(function ($row) use ($request) {
return (new ClientEntityResource($row))->toArray($request);
})
];
}
Run Code Online (Sandbox Code Playgroud)
客户端实体资源
public function toArray($request)
{
$data = [
'type' => 'cliententity',
'id' => $this->clienteEntidadeId,
'clienteEntidadeNome' => $this->clienteEntidadeNome,
];
return $data;
}
Run Code Online (Sandbox Code Playgroud)
然后,在我的控制器中我可以调用完整列表:
return (new ClientEntityCollection($rows))
->response()
->setStatusCode(200);
Run Code Online (Sandbox Code Playgroud)
或者单个项目:
return (new ClientEntityResource($row))
->response()
->setStatusCode(200);
Run Code Online (Sandbox Code Playgroud)
它的工作原理就像一个魅力,有分页等等。