我尝试在我的资源中构建自定义响应,如下所示:
class ApplicationResource extends JsonResource
{
/**
* Transform the resource into an array.
*
* @param \Illuminate\Http\Request $request
* @return array
*/
public function toArray($request)
{
return [
'id' => $this->id,
'sort'=> $this->sort,
'is_seen' => $this->is_seen,
'name' => $this->name,
'position' => $this->position,
'company' => $this->company,
'education' => $this->education,
'degree' => $this->degree,
'phone' => $this->phone,
'university' => $this->university,
'cv_folder_id' => $this->cv_folder_id,
'cv' => route('applications.cvShow', ['candidateCv' => $this->candidate_cv]),
'comments'=> ApplicationCommentsResource::collection($this->applicationComments),
'ratingFields'=> ApplicationRatingsResource::collection($this->applicationRatings()->get()),
'jobPostRatingFields' => JobPostRatingFieldsResource::collection($this->jobPost->jobPostRatingFields),
];
}
}
但我只是得到错误。我得到的错误是:
在 null 上调用成员函数 first()
我不知道如何建立我的响应,如果集合为空,我不会收到任何错误?