Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

sarfaraz1212's avatar

Pagination data not coming in api resource

i want to use api resource in my function

 function get($id = null,$search = null)
    {
        try {
            $query = User::with('details', 'documents', 'pictures')
                ->where('id', '!=', 1);

            if ($id) {
                return $query->where('id', $id)->firstOrFail();
            }

            if ($search) {
                $query->where(function ($q) use ($search) {
                    $q->where('name', 'like', '%' . $search . '%')
                      ->orWhere('email', 'like', '%' . $search . '%')
                      ->orWhereHas('details', function ($detailsQuery) use ($search) {
                          $detailsQuery->where('phone_number', 'like', '%' . $search . '%');
                      });
                });
            }

            return $query->paginate(5);
        } catch (\Throwable $th) {
            throw new ApiException('An error occurred while getting trainer details.', 400, $th, "TrainerService : get");
        }
    }

The issue is when i don't do

 return TrainerResource::collection( $query->paginate(5));

it works fine the fields like current_page,last_Page etc come, However if i apply resource to it they go away.

what can i do to prevent it?

0 likes
7 replies
tykus's avatar

The collection method should accept a Paginator instance returning a Response with a JSON object having data (your models), links (first, last, prev and next page links) and meta (pagination-specific information) keys:

{
    "data": [],
    "links":{
        "first": "http://example.com/users?page=1",
        "last": "http://example.com/users?page=1",
        "prev": null,
        "next": null
    },
    "meta":{
        "current_page": 1,
        "from": 1,
        "last_page": 1,
        "path": "http://example.com/users",
        "per_page": 15,
        "to": 10,
        "total": 10
    }
}

What does your JSON look like whenever you use the Resource?

sarfaraz1212's avatar

@tykus

with resource

without resource

My code

function get($id = null,$search = null)
    {
        try {
            $query = User::with('details', 'documents', 'pictures')
                ->where('id', '!=', 1);

            if ($id) {
                return $query->where('id', $id)->firstOrFail();
            }

            if ($search) {
                $query->where(function ($q) use ($search) {
                    $q->where('name', 'like', '%' . $search . '%')
                      ->orWhere('email', 'like', '%' . $search . '%')
                      ->orWhereHas('details', function ($detailsQuery) use ($search) {
                          $detailsQuery->where('phone_number', 'like', '%' . $search . '%');
                      });
                });
            }

            //return TrainerResource::collection( $query->paginate(5));
            return $query->paginate(5);
        } catch (\Throwable $th) {
            throw new ApiException('An error occurred while getting trainer details.', 400, $th, "TrainerService : get");
        }
    }
tykus's avatar

@sarfaraz1212 it looks like you're customising the response payload; show us the TrainerResource class.

sarfaraz1212's avatar

@tykus

<?php

namespace App\Http\Resources;

use Illuminate\Http\Request;
use Illuminate\Http\Resources\Json\JsonResource;

class TrainerResource extends JsonResource
{
    public function toArray(Request $request): array
    {
        return parent::toArray($request);
    }
}

right now i have not changed anything in the resource file, but i will add picture and document urls in their respective objects

Snapey's avatar

In the code you showed, where does "Trainers fetched successfully!" get added ?

Are you sure you are hitting this route?

Mahmoud04's avatar
Mahmoud04
Best Answer
Level 2

@sarfaraz1212

just use this:

 return TrainerResource::collection( $query->paginate(5))->response()->getData(true);

Please or to participate in this conversation.