I have 2 routes which hits show() & index() method in controller. The model name is Feed & has 4 relations FeedType, FeedImage, FeedVideo & FeedCaption.
I m returning all the fields & specific relations fields except 'thumbnail' field through the Feed resource when API hits show() method
class Feed extends JsonResource
{
/**
* Transform the resource into an array.
*
* @param IlluminateHttpRequest $request
* @return array
*/
public function toArray($request)
{
return [
'id' => $this->id,
'title' => $this->title,
'content' => $this->content,
'keywords' => $this->keywords,
'slug' => $this->slug,
'type' => new FeedTypeResource($this->feedType),
'images' => FeedImageResource::collection($this->feedImages),
'video' => new FeedVideoLinkResource($this->feedVideoLink),
'caption' => new FeedCaptionResource($this->feedCaption),
];
}
}
Now I want to return only 3 fields 'title', 'thumbnail', 'created_at' with one relation field 'name' from FeedType relation through a resource when API hits show() method. So I created a FeedCollection resource. But it is giving error.
class FeedCollection extends ResourceCollection
{
/**
* Transform the resource collection into an array.
*
* @param IlluminateHttpRequest $request
* @return array
*/
public function toArray($request)
{
return [
'title' => $this->title,
'thumbnail' => $this->thumbnail,
];
}
}
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…