我正在尝试创建一个 API,该 API 将从数据库返回所有客户记录。但这提供了分页和过滤功能。,
过滤功能是一个可选的查询参数。因此不必将其包含在查询参数中。
但我在这样做时遇到了问题。
这是 CustomerController 文件中的索引方法:
public function index(Request $request)
{
// Get how many item per page
$itemPerPage = $request->query('per_page');
// SQL Query
$customers = Customer::all();
// Filter data
if (!empty($request->name)) {
$customers = $customers->where('name', '=', $request->name);
}
// Return the result as JSON
return new CustomerCollection($customers->paginate($itemPerPage));
}
或者有更好的方法将可选过滤功能与分页相结合吗?
谢谢。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号