0

我尝试在我的资源中构建自定义响应,如下所示:

    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()

我不知道如何建立我的响应,如果集合为空,我不会收到任何错误?

4

1 回答 1

1

这仅仅意味着您想要检索不存在的值。您可以像这样制作简单的条件:

if(is_null($this->sort)){
    return "-";
}

祝你好运!

于 2019-11-04T06:54:39.447 回答