我想修正流明设置页码的方式。我需要第一页为0,而不是默认为1。
因此,当我将端点称为http://localhost:3000/persons?page=0
时,结果如下:
class PersonController extends Controller
{
public function index(Request $request)
{
$personList = Person::paginate($request->query('per_page') ?? 10);
return $personList;
}
}
{
"current_page": 1, // this should be 0
"data": [],
"first_page_url": "http://localhost:7353/persons?page=1", // this should be 0
"from": 1, // this should be 0
"last_page": 31, // this should be 30
"last_page_url": "http://localhost:7353/persons?page=31", // this should be 30
"next_page_url": "http://localhost:7353/persons?page=2",
"path": "http://localhost:7353/persons",
"per_page": 10,
"prev_page_url": null,
"to": 10,
"total": 310
}
这是因为我正在使用从零开始的材质ui分页器。
<TablePagination
component='div'
rowsPerPageOptions={[5, 10, 25]}
colSpan={3}
count={this.state.pagination.total}
rowsPerPage={this.state.pagination.per_page}
page={this.state.pagination.page}
onChangePage={this.handleChangePage}
onChangeRowsPerPage={this.handleChangeRowsPerPage} />
docs说关于page
道具:
当前页面的从零开始的索引。
我不想通过在所有我对项目进行分页的地方进行补偿来解决此问题。因此,当调用paginate
方法时,我需要后端返回索引为零的页面,并且据我所知,可以按docs的说明完成此操作,但我不知道该怎么做。
提前感谢。
您可以尝试偏移请求页面:
class PersonController extends Controller
{
public function index(Request $request)
{
$request->merge([
'page' => $request->input('page', 0) + 1
]);
$personList = Person::paginate($request->query('per_page') ?? 10);
return $personList;
}
}