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

jefpmn's avatar

"Undefined property: Illuminate\\Pagination\\LengthAwarePaginator::$<column_name_here>" when passing LengthAwarePaginator instance to ResourceCollection

I got this error when trying to follow this guide.

{
    "message": "Undefined property: Illuminate\Pagination\LengthAwarePaginator::$id",
    "exception": "ErrorException",
    "file": "/var/www/biarlaku/vendor/laravel/framework/src/Illuminate/Http/Resources/DelegatesToResource.php",
    "line": 116,
    "trace": [ ... ] 
}

Here's the code that can reproduce the same error:

ItemCategoryController.php

public function index(ResourceIndexRequest $request)
{
        $categories = new MarketplaceItemCategory;

        if($request->has('search'))
        {
            $categories->where('name', '=', $request->search);
        }

        return new ItemCategoryCollection($categories->paginate($request->per_page ?? 15));
}

ResourceCollection.php

<?php

namespace App\Http\Resources;

use Illuminate\Http\Request;
use Illuminate\Http\Resources\Json\ResourceCollection as BaseResourceCollection;

class ResourceCollection extends BaseResourceCollection
{
    protected $fields;

    protected function _parseFieldRequest($request)
    {
        $fields = explode(",", $request->fields);

        array_walk($fields, function (&$item) {
            $item = trim($item);

            if(str_contains($item, ":"))
            {
                list($fieldName, $attribute) = explode(":", $item);

                $item = ['field' => $fieldName, 'attribute' => $attribute];
            }
        });

        $this->fields = $fields;
    }

    protected function _isFieldRequested($fieldName)
    {
        foreach ($this->fields as $field) {
            if(is_array($field))
            {
                if($field['fieldName'] === $fieldName)
                {
                    return true;
                }
            }

            if($field === $fieldName)
            {
                return true;
            }
        }

        return false;
    }

    protected function _formatTimestamp($datetime, $fieldName)
    {
        $request = new Request;

        if($request->has('timestamps_format'))
        {
            return $datetime->format($request->timestamps_format);
        }

        foreach ($this->fields as $field) {
            if(is_array($field))
            {
                if($field['fieldName'] === $fieldName)
                {
                    return $datetime->format($field['attribute']);
                }
            }
        }

        return $datetime;
    }

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

ItemCategoryCollection.php

<?php

namespace App\Http\Resources\Marketplace;

use App\Http\Resources\ResourceCollection;

class ItemCategoryCollection extends ResourceCollection
{
    /**
     * Transform the resource collection into an array.
     *
     * @param  \Illuminate\Http\Request
     * @return array
     */
    public function toArray($request)
    {
        $this->_parseFieldRequest($request);

        if($request->has('fields'))
        {
            return [
                'id' => $this->when($this->_isFieldRequested('id'), $this->id),
                'name' => $this->when($this->_isFieldRequested('name'), $this->name),
                'slug' => $this->when($this->_isFieldRequested('slug'), $this->slug),
                'metafields' => $this->when($this->_isFieldRequested('metafields'), $this->metafields),
                'created_at' => $this->when($this->_isFieldRequested('created_at'), $this->_formatTimestamp($this->created_at, 'created_at')),
                'updated_at' => $this->when($this->_isFieldRequested('updated_at'), $this->_formatTimestamp($this->updated_at, 'updated_at'))
            ];
        }

        return [
            'id' => $this->id,
            'name' => $this->name,
            'slug' => $this->slug,
            'metafields' => $this->metafields,
            'created_at' => $this->_formatTimestamp($this->created_at, 'created_at'),
            'updated_at' => $this->_formatTimestamp($this->updated_at, 'updated_at')
        ];

    }
}

Is this bug or i do it wrong?

0 likes
1 reply
jefpmn's avatar
jefpmn
OP
Best Answer
Level 1

Solved. At first, I confused with difference between Resource and ResourceCollection. Actually, Illuminate\Http\Resources\Json\ResourceCollection only had property $this->collection. So instead to use that, i'm using single resource Illuminate\Http\Resources\Json\Resource and make like this : ItemCategoryController.php

public function index(ResourceIndexRequest $request)
    {
        $categories = new MarketplaceItemCategory;

        if($request->has('search'))
        {
            $categories->where('name', '=', $request->search);
        }

        return ItemCategory::collection($categories->paginate($request->per_page ?? 15));
    }

I hope this would help anyone who using Eloquent API Resources and stuck in same problem.

Please or to participate in this conversation.