Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

962
Views
How to add meta data in laravel api resource

I using laravel api resource for my FRONT-END site.

Actually i using also laravel pagination to handle paginate in FRONT-END.

The api resource structure like this:

{
    "data": [...],
    "links": [...], // handle pagination
    "meta": [...] // handle pagination
}

But i want to send count of query results without considering paginating .For example i paginating database result into 30 pre_page and the all of query result is 200 and i want to send this count to view front-end

I decided to get count of query result and give to resource collection and restructure laravel api resource to this.

{
    "data": [...],
    "metadata": {
        "count": 200
    },
    "links": [...], // handle pagination
    "meta": [...] // handle pagination
}

Also you sould know maybe i want to add another data to this collection. for example conversion rate in addition count to metadata and i don't know how do this.

over 4 years ago · Santiago Trujillo
2 answers
Answer question

0

I find the way hou can i do this

Actually i just have to use additional method for laravel resource like:

$data = Project::limit(100)->get();
return ProjectResource::collection($data)->additional(['some_id => 1']);
over 4 years ago · Santiago Trujillo Report

0

See top-level-meta-data.

You need to create a ResourceCollection and then define the additional meta variables in the with function.

<?php

namespace App\Http\Resources;

use Illuminate\Http\Resources\Json\ResourceCollection;

class UserCollection extends ResourceCollection
{
    /**
    * Transform the resource collection into an array.
    *
    * @param  \Illuminate\Http\Request  $request
    * @return array
    */
    public function toArray($request)
    {
        return parent::toArray($request);
    }

    /**
    * Get additional data that should be returned with the resource array.
    *
    * @param  \Illuminate\Http\Request  $request
    * @return array
    */
    public function with($request)
    {
        return [
            'meta' => [
                'key' => 'value',
            ],
        ];
    }
}
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!