与Laravel API资源的分页加载关系
您好,我正在加载laravel 5.6中API资源上的关系:与Laravel API资源的分页加载关系,laravel,laravel-5.6,Laravel,Laravel 5.6,您好,我正在加载laravel 5.6中API资源上的关系: public function show($id) { return new YardResource(Yard::find($id)->with('inspections')->paginate(1)); } 码源 public function toArray($request) { return [ 'id' => $this->id,
public function show($id)
{
return new YardResource(Yard::find($id)->with('inspections')->paginate(1));
}
码源
public function toArray($request)
{
return [
'id' => $this->id,
'name' => $this->name,
'description' => $this->description,
'maf_id' => $this->maf_id,
'street' => $this->street,
'suburb' => $this->suburb,
'city' => $this->city,
'country' => $this->country,
'lat' => $this->lat,
'long' => $this->long,
'landowner_name' => $this->landowner_name,
'landowner_phone' => $this->landowner_phone,
'landowner_phone2' => $this->landowner_phone2,
'landowner_mobile' => $this->landowner_mobile,
'landowner_mobile2' => $this->landowner_mobile2,
'landowner_email' => $this->landowner_email,
'landowner_street' => $this->landowner_street,
'landowner_suburb' => $this->landowner_suburb,
'landowner_city' => $this->landowner_city,
'landowner_country' => $this->landowner_country,
'landowner_postcode' => $this->landowner_postcode,
'acquired_at' => $this->acquired_at,
'quarantined' => $this->quarantined,
'archived' => $this->archived,
'latest_inspection' => new Inspection($this->latestInspection),
'inspections' => InspectionResource::collection($this->whenLoaded('inspections')),
'tasks' => TaskResource::collection($this->tasks->where('completed', false)), // Get active tasks
'feedings' => FeedResource::collection($this->feeds),
'diseases' => DiseaseResource::collection($this->diseases),
'treatments' => TreatmentResource::collection($this->activeTreatments), // between dates
'yard_events' => YardEventResource::collection($this->yardEvents),
];
}
我想对“检查”关系进行分页,但运气不好
出现以下错误:
Undefined property: Illuminate\Pagination\LengthAwarePaginator::$id
任何帮助都会很好 分页对集合有效,似乎toArray方法无法更改分页结果,因此请将普通集合更改为所需数组,然后将数组更改为集合,最后对其进行分页。 我想你可以做这样的事
$resultArray = new YardResource(Yard::find($id)->with('inspections')); // if your toArray method supports any size of objects
$resultCollecttion = collect($resultArray);
return $resultCollection->paginate(1);
这个问题很可能不是资源层本身,而是模型中实体的关系级故障。验证检验员模型是否与堆场正确相关 如果没有问题,那么在不添加关系的情况下测试资源的响应
returnnewyardresource(Yard::find($id)->paginate(1))代码>
然后您可以放弃问题的确切来源错误消息是什么?@MasoudHaghbin抱歉,刚才添加了使用first()和what is YardResource更改分页(1)?@MasoudHaghbin first()将获取第一项,但我希望能够对结果进行分页。添加了码源