JsonResource 和 ResourceCollection 有什么区别?在 Laravel v6 或 v7 中
What is the difference between a JsonResource & ResourceCollection? in Laravel v6 or v7
谁能解释一下 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);
}
}
对比...
<?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);
}
}
ResourceCollection
用于 return 项目列表,而不是 JsonResource
用于 return 单个项目。
这是我的一个项目中的一段代码,我有一个文章列表,我使用了 ResourceCollection
到 return 一系列文章:
<?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
];
}
}
此代码 return 是一个文章列表。
然后当用户点击一篇文章时,我必须向用户显示一篇文章,所以我必须 return 一篇文章,我使用了以下代码:
<?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
];
}
}
您可以看到与您的问题相关的
希望你觉得有用。
集合用于多个资源。
我习惯这样做,例如:
ClientEntityCollection:
public function toArray($request)
{
return [
'data' => $this->collection->map(function ($row) use ($request) {
return (new ClientEntityResource($row))->toArray($request);
})
];
}
ClientEntityResource
public function toArray($request)
{
$data = [
'type' => 'cliententity',
'id' => $this->clienteEntidadeId,
'clienteEntidadeNome' => $this->clienteEntidadeNome,
];
return $data;
}
然后,在我的控制器中,我可以调用完整列表:
return (new ClientEntityCollection($rows))
->response()
->setStatusCode(200);
或单个项目:
return (new ClientEntityResource($row))
->response()
->setStatusCode(200);
它就像一个魅力,分页和所有..
谁能解释一下 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);
}
}
对比...
<?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);
}
}
ResourceCollection
用于 return 项目列表,而不是 JsonResource
用于 return 单个项目。
这是我的一个项目中的一段代码,我有一个文章列表,我使用了 ResourceCollection
到 return 一系列文章:
<?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
];
}
}
此代码 return 是一个文章列表。 然后当用户点击一篇文章时,我必须向用户显示一篇文章,所以我必须 return 一篇文章,我使用了以下代码:
<?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
];
}
}
您可以看到与您的问题相关的
希望你觉得有用。
集合用于多个资源。
我习惯这样做,例如:
ClientEntityCollection:
public function toArray($request)
{
return [
'data' => $this->collection->map(function ($row) use ($request) {
return (new ClientEntityResource($row))->toArray($request);
})
];
}
ClientEntityResource
public function toArray($request)
{
$data = [
'type' => 'cliententity',
'id' => $this->clienteEntidadeId,
'clienteEntidadeNome' => $this->clienteEntidadeNome,
];
return $data;
}
然后,在我的控制器中,我可以调用完整列表:
return (new ClientEntityCollection($rows))
->response()
->setStatusCode(200);
或单个项目:
return (new ClientEntityResource($row))
->response()
->setStatusCode(200);
它就像一个魅力,分页和所有..