Answer the question
In order to leave comments, you need to log in
How to hide fields in Collection?
There is a controller
public function index(): ArticleCollection
{
$articles = Article::orderByDesc('id')->paginate(24);
return new ArticleCollection($articles);
}
class ArticleCollection extends ResourceCollection
{
/**
* Transform the resource collection into an array.
*
* @param \Illuminate\Http\Request $request
* @return array|\Illuminate\Contracts\Support\Arrayable|\JsonSerializable
*/
public function toArray($request)
{
return [
'data' => $this->collection,
];
}
}
"links": {
"first": "http://site.test/api/v1/admin/articles?page=1",
"last": "http://site.test/api/v1/admin/articles?page=1",
"prev": null,
"next": null
},
"meta": {
"current_page": 1,
"from": 1,
"last_page": 1,
"links": [
{
"url": null,
"label": "« Previous",
"active": false
},
{
"url": "http://site.test/api/v1/admin/articles?page=1",
"label": "1",
"active": true
},
{
"url": null,
"label": "Next »",
"active": false
}
],
"path": "http://site.test/api/v1/admin/articles",
"per_page": 24,
"to": 1,
"total": 1
}
Answer the question
In order to leave comments, you need to log in
class ArticleCollection extends ResourceCollection
{
/**
* Transform the resource collection into an array.
*
* @param \Illuminate\Http\Request $request
* @return array|\Illuminate\Contracts\Support\Arrayable|\JsonSerializable
*/
public function toArray($request)
{
return [
'data' => [
'id' => $this->id,
................
],
];
}
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question