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?