使用Laravel创建API资源
下面是我的数据,我想用这个创建API资源-使用Laravel创建API资源,laravel,rest,doctrine,Laravel,Rest,Doctrine,下面是我的数据,我想用这个创建API资源- array:1 [ 0 => JobPosted {#2635 -id: 1 -jobTitle: "Business Development Executive" -noOfPosition: 1 -experience: "2-5" -jobLocation: "Mumbai" -jobSkill: "Good communication skill, knowledge of Mandar
array:1 [
0 => JobPosted {#2635
-id: 1
-jobTitle: "Business Development Executive"
-noOfPosition: 1
-experience: "2-5"
-jobLocation: "Mumbai"
-jobSkill: "Good communication skill, knowledge of Mandarin"
-jobDescription: "Good communication skill, knowledge of Mandarin"
-isClosed: false
-isActive: true
}
]
我在用拉威尔的教义。
我的工作资源是
public function toArray($request){
return [
'jobTitle' => $this->getJobTitle(),
'noOfPosition' => $this->getNoOfPosition(),
'experience' => $this->getExperience(),
'jobLocation' => $this->getJobLocation(),
'jobSkills' => $this->getJobSkill(),
'jobDescription' => $this->getJobDescription()
];
}
JobResourceCollection如下所示
public function toArray($request){
return parent::toArray($request);
}
而JobController是
public function getJobs(Request $request){
$jobs = $this->jobPostedService->getJobPosted($limit);
return response()->json(new JobResourceCollection($jobs), 200);
}
执行此操作后,我得到的错误如下
Symfony\Component\Debug\Exception\FatalThroTableError
(E_ERROR)对数组上的成员函数first()的调用
通过使用解决了该问题
public function getJobs(Request $request){
$jobs = $this->jobPostedService->getJobPosted($limit);
return response()->json(new JobResourceCollection(collect($jobs)), 200);
}
而不是
public function getJobs(Request $request){
$jobs = $this->jobPostedService->getJobPosted($limit);
return response()->json(new JobResourceCollection($jobs), 200);
}
请使用jobpostedservice上的getJobPosted方法