Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel API ResourceCollection doesnt works

i would like to know how to get work API call with ResourceCollection

Error :

Undefined property: Illuminate\Database\Query\Builder::$name

my product method :

 public function index()
{
    return ProductCollection::collection(Product::all());
}

my collection :

public function toArray($request)
{
    //return parent::toArray($request);
    return [
        "name" => $this->name,
        "price" => $this->price,
        "href" => [
            "link" => route("product.show", $this->id)
        ]
    ];
}

tryed it just by Resourse ( not ResourceCollection ) just modifed method call and its was working but i need know how to fix ResourceCollection return new ProductResource($product);

error snap :

enter image description here

like image 915
Patrik Horváth Avatar asked Mar 07 '23 10:03

Patrik Horváth


1 Answers

Change

use Illuminate\Http\Resources\Json\ResourceCollection;

class ProductCollection extends ResourceCollection
{

to

use Illuminate\Http\Resources\Json\Resource;

class ProductCollection extends Resource
{
like image 133
carlos barrera Avatar answered Apr 28 '23 06:04

carlos barrera