在加載網(wǎng)站素材資源時候:且還是滾動加載的情況;
使用 Fetch POST JSON
形式請求數(shù)據(jù)
使用游標(biāo)分頁
Material::where(['type'=>'image'])->orderBy('uuid','desc')->cursorPaginate(5);
返回
{
"path": "/image",
"per_page": 5,
"next_cursor": "eyJ1dWlkIjoxMDA2LCJfcG9pbnRzVG9OZXh0SXRlbXMiOnRydWV9",
"next_page_url": "?cursor=eyJ1dWlkIjoxMDA2LCJfcG9pbnRzVG9OZXh0SXRlbXMiOnRydWV9",
"prev_cursor": null,
"prev_page_url": null
}
下一頁請求:
Material::where(['type'=>'image'])->orderBy('uuid','desc')->cursorPaginate(3, '*', $cursor);
3
每頁分頁數(shù)量*
要返回那些字段$cursor
不是你給他傳變量:而是告訴它去 $request->input(NAME)
的NAME
就行了文檔還是翻得太少~
在 Model
中 protected $casts = 應(yīng)該可以定義一些字段
模型中:
getCursorName
可以定義第三個參數(shù)
public function getCursorName(): string
{
return 'cursor_other';
}
在 \vendor\illuminate\pagination\AbstractCursorPaginator.php
還有些參數(shù)可以定義
Material::query()
->where(['type'=>'image'])
->orderBy('uuid','desc')
->cursorPaginate($request->query('per_page', 20))
->appends($request->query());