A
A
Analka2022-02-18 16:19:58
Laravel
Analka, 2022-02-18 16:19:58

How to hide fields in Collection?

There is a controller

public function index(): ArticleCollection
    {
        $articles = Article::orderByDesc('id')->paginate(24);
        
        return new ArticleCollection($articles);
    }


and resource

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,
        ];
    }
}


how do i hide the

array when pushing to the front

"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
    },


and remove links from meta

"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

1 answer(s)
P
pLavrenov, 2022-02-18
@pLavrenov

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 question

Ask a Question

731 491 924 answers to any question