Laravel 5.5 API resources for collections (standalone data)

前端 未结 3 1639
悲&欢浪女
悲&欢浪女 2020-12-14 10:15

I was wondering if it is possible to define different data for item resource and collection resource.

For collection I only want to send [\'id\', \'title\', \

3条回答
  •  轮回少年
    2020-12-14 10:37

    If you want the response fields to have the same value in the Resource and Collection, you can reuse the Resource inside the Collection

    PersonResource.php

     $this->id,
                'person_type' => $this->person_type,
                'first_name' => $this->first_name,
                'last_name' => $this->last_name,
                'created_at' => (string) $this->created_at,
                'updated_at' => (string) $this->updated_at,
            ];
        }
    }
    

    PersonCollection.php

    collection);
        }
    }
    

提交回复
热议问题