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

tayyabshahzad1's avatar

API resource Laravel

Hi All,

Simple API resource and give me an error

"message": "Undefined property: Illuminate\\Database\\Query\\Builder::$id",

Controller Code

 return PromotionCollection::collection($promotions); 

AND PromotionCollection

public function toArray($request)
    {
        return [
            'id' => $this->id,
        ];
    }
0 likes
6 replies
Sinnbeck's avatar

Seems like you forgot to terminate your query

 return PromotionCollection::collection($promotions->get()); 
Sinnbeck's avatar

@tayyabshahzad1 Why are you btw calling ::collection on a collection resource? Did you mean to do

 return new PromotionCollection($promotions); 
tayyabshahzad1's avatar

@Sinnbeck Yes this is working but I have a promotion table with a lot of columns in Api response I only want to return 2 3 columns data in my all collection e.g name, type, date.

Please or to participate in this conversation.