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

Prabodhana's avatar

API Resource paginate

I'm using one API resource file to send data and inside that file I want to send collection on paginate resource. But I cant get only Attributes what I need it getting this error.

 "message": "Undefined property: Illuminate\Pagination\LengthAwarePaginator::$id",

this is my two resources files

CourierCashBookResourceWithBal

class CourierCashBookResourceWithBal extends JsonResource
{
    /**
     * Transform the resource into an array.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return array
     */
    public function toArray($request)
    {
        //return parent::toArray($request);
        return [
            'balance' => $this->balance,
            'TotalCr' => $this->cr,
            'TotalDr' => $this->dr,
            'items' => new CourierCashBookResource(tblCourierCashBook::with('courier')->paginate()),
           
        ];
    }
}

CourierCashBookResource

class CourierCashBookResource extends JsonResource
{
    /**
     * Transform the resource into an array.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return array
     */
    public function toArray($request)
    {
        //return parent::toArray($request);
        return [
            'id' => $this->id,
            'nickName' => $this->courier->nickName,
            'des' => $this->des,
            'cr' => $this->cr,
            'dr' => $this->dr,
        ];
    }
}

thank you..

0 likes
2 replies
bugsysha's avatar

I've never used it that way. I've paginated in the controller and passed that result to the resource.

Prabodhana's avatar

If I paginate in controller it get all data in the row.. I want to response few data of them. how can I manage it in resource file? (not using select query in controller)

Please or to participate in this conversation.