I am trying to create an API that will return all customers record from the database. But this provides pagination and filtering.,
过滤功能是一个可选的查询参数.所以不需要将其包含在查询参数中.
但我在做这件事时面临一个问题.
以下是我在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));
}
或者有更好的方法将可选过滤功能与分页结合起来吗?
非常感谢.