当我想在控制器内为 listStory() 方法生成分页时,我注意到这个问题开始了。当我调查时,我发现 #items 是空的,最初不是。它正在正确计算总数。
在我的调查中,我尝试过:
$allStoryByTeller = Story::all();
dd($allStoryByTeller);
它返回:
Collection {#344 ▼
#items: array:10 [▼
0 => Story {#345 ▶}
1 => Story {#346 ▶}
2 => Story {#347 ▶}
3 => Story {#348 ▶}
4 => Story {#349 ▶}
5 => Story {#350 ▶}
6 => Story {#351 ▶}
7 => Story {#352 ▶}
8 => Story {#353 ▶}
9 => Story {#354 ▶}
]
}
这应该是以下输出中的#items包含的内容:
public function listStory()
{
$allStoryByTeller = Story::with([
'user'
])->paginate($perPageCount = 10);
dd($allStoryByTeller);
...
}
上面的这段代码返回:
LengthAwarePaginator {#316 ▼
#total: 9
#lastPage: 1
#items: Collection {#318 ▼
#items: []
}
#perPage: 10
#currentPage: 2
#path: "http://127.0.0.1:8000/v1/stories/list"
#query: []
#fragment: null
#pageName: "page"
+onEachSide: 3
#options: array:2 [▶]
}
我希望#items 包含 9 个模型,而不是 [] 为空,因为正确计算了总数。