用于集合的 Laravel 5.5 API 资源(独立数据)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/46388205/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Laravel 5.5 API resources for collections (standalone data)
提问by Dorin Niscu
I was wondering if it is possible to define different data for item resource and collection resource.
我想知道是否可以为项目资源和集合资源定义不同的数据。
For collection I only want to send ['id', 'title', 'slug']
but the item resource will contain extra details ['id', 'title', 'slug', 'user', etc.]
对于收藏,我只想发送['id', 'title', 'slug']
但项目资源将包含额外的详细信息['id', 'title', 'slug', 'user', etc.]
I want to achieve something like:
我想实现以下目标:
class PageResource extends Resource
{
/**
* Transform the resource into an array.
*
* @param \Illuminate\Http\Request
* @return array
*/
public function toArray($request)
{
return [
'id' => $this->id,
'title' => $this->title,
'slug' => $this->slug,
'user' => [
'id' => $this->user->id,
'name' => $this->user->name,
'email' => $this->user->email,
],
];
}
}
class PageResourceCollection extends ResourceCollection
{
/**
* Transform the resource collection into an array.
*
* @param \Illuminate\Http\Request
* @return array
*/
public function toArray($request)
{
return [
'id' => $this->id,
'title' => $this->title,
'slug' => $this->slug,
];
}
}
PageResourceCollectionwill not work as expected because it uses PageResourceso it needs
PageResourceCollection不会按预期工作,因为它使用PageResource所以它需要
return [
'data' => $this->collection,
];
I could duplicate the resource into PageFullResource
/ PageListResource
and PageFullResourceCollection
/ PageListResourceCollection
but I am trying to find a better way to achieve the same result.
我可以将资源复制到PageFullResource
/PageListResource
和PageFullResourceCollection
/PageListResourceCollection
但我试图找到一种更好的方法来实现相同的结果。
回答by Jeff Puckett
The Resource class has a collection method on it. You can return that as the parameter input to your ResourceCollection, and then specify your transformations on the collection.
Resource 类有一个集合方法。您可以将其作为参数输入返回到您的 ResourceCollection,然后在集合上指定您的转换。
Controller:
控制器:
class PageController extends Controller
{
public function index()
{
return new PageResourceCollection(PageResource::collection(Page::all()));
}
public function show(Page $page)
{
return new PageResource($page);
}
}
Resources:
资源:
class PageResource extends Resource
{
public function toArray($request)
{
return [
'id' => $this->id,
'title' => $this->title,
'slug' => $this->slug,
'user' => [
'id' => $this->user->id,
'name' => $this->user->name,
'email' => $this->user->email,
],
];
}
}
class PageResourceCollection extends ResourceCollection
{
public function toArray($request)
{
return [
'data' => $this->collection->transform(function($page){
return [
'id' => $page->id,
'title' => $page->title,
'slug' => $page->slug,
];
}),
];
}
}
回答by cyberfly
If you want the response fields to have the same value in the Resource and Collection, you can reuse the Resource inside the Collection
如果希望资源和集合中的响应字段具有相同的值,则可以重用集合内的资源
PersonResource.php
人员资源.php
<?php
namespace App\Http\Resources;
use Illuminate\Http\Resources\Json\Resource;
class PersonResource extends Resource
{
/**
* Transform the resource into an array.
*
* @param \Illuminate\Http\Request $request
* @return array
*/
public function toArray($request)
{
// return parent::toArray($request);
return [
'id' => $this->id,
'person_type' => $this->person_type,
'first_name' => $this->first_name,
'last_name' => $this->last_name,
'created_at' => (string) $this->created_at,
'updated_at' => (string) $this->updated_at,
];
}
}
PersonCollection.php
人物集合.php
<?php
namespace App\Http\Resources;
use Illuminate\Http\Resources\Json\ResourceCollection;
class PersonCollection extends ResourceCollection
{
/**
* Transform the resource collection into an array.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Resources\Json\AnonymousResourceCollection
*/
public function toArray($request)
{
// return parent::toArray($request);
return PersonResource::collection($this->collection);
}
}
回答by Wale
The accepted answer works, if you are not interested in using links and meta data. If you want, simply return:
如果您对使用链接和元数据不感兴趣,则接受的答案有效。如果需要,只需返回:
return new PageResourceCollection(Page::paginate(10));
in your controller. You should also look to eager load other dependent relationships before passing over to the resource collection.
在您的控制器中。您还应该在传递给资源集合之前预先加载其他依赖关系。