在 laravel 资源中调用 null 的成员函数 first()
Call to a member function first() on null in laravel resource
我尝试像这样在我的资源中构建自定义响应:
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),
];
}
}
但我只是收到错误。我得到的错误是:
Call to a member function first() on null
我不知道如何建立我的响应,如果集合为空我不会收到任何错误?
这只是意味着您要检索不存在的值。
你可以像这样制作简单的条件:
if(is_null($this->sort)){
return "-";
}
祝你好运!
我尝试像这样在我的资源中构建自定义响应:
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),
];
}
}
但我只是收到错误。我得到的错误是:
Call to a member function first() on null
我不知道如何建立我的响应,如果集合为空我不会收到任何错误?
这只是意味着您要检索不存在的值。 你可以像这样制作简单的条件:
if(is_null($this->sort)){
return "-";
}
祝你好运!