我在Laravel有一张表格。我在此表单和搜索选项中也使用了pagination
。当我没有搜索任何东西时,分页工作正常。如果我搜索记录超过10的内容(我的分页记录限制为10),它会在第一页中完美显示结果,但在第二页或第三页中显示整个数据库的记录。它忽略了搜索条件。
控制器代码:
public function index()
{
if (Input::has('invoiceNumber')) {
$invoiceNumber = Input::get('invoiceNumber');
$invoices = Invoice::where('invoiceNumber','like', '%'.$invoiceNumber.'%')->orderBy('date','desc')->paginate(10);
} elseif (Input::has('client')) {
$client = Input::get('client');
$invoices = Invoice::where('client','like', '%'.$client.'%')->orderBy('date', 'desc')->paginate(10);
} elseif (Input::has('startDate') && Input::has('endDate')) {
$startDate = Input::get('startDate');
$endDate = Input::get('endDate');
$invoices = Invoice::whereBetween('date', [$startDate, $endDate])->orderBy('date', 'desc')->paginate(10);
} elseif (Input::has('startDate')) {
$startDate = Input::get('startDate');
$invoices = Invoice::where('date', $startDate)->orderBy('date', 'desc')->paginate(10);
} elseif (Input::has('endDate')) {
$endDate = Input::get('endDate');
$invoices = Invoice::where('date', $endDate)->orderBy('date', 'desc')->paginate(10);
} else {
$invoices = DB::table('invoices')
->join('vehicles', 'invoices.vehicle', '=', 'vehicles.registration')
->select(
'invoices.*',
'vehicles.brand',
'vehicles.model',
'vehicles.seat',
'vehicles.remarks'
)
->orderBy('date', 'desc')
->paginate(10);
}
return view('invoice.index',compact('invoices'));
}
执行搜索后,我的网址如下所示:
http://my-site.com/invoice?invoiceNumber=&client=&startDate=2016-01-01&endDate=2016-02-08
点击第2页后进行此搜索后,网址将变为:
http://my-site.com/invoice?page=2
答案 0 :(得分:2)
尝试使用此波纹管代码(根据您的要求进行更改)
return view('invoice.index')->withInvoices($invoices->appends(Input::except('page'));
希望它能为你效劳......
答案 1 :(得分:1)
在您的视图文件中,渲染必须如下所示:
{!! $invoices->appends(Input::except('page'))->render() !!}
答案 2 :(得分:0)
只需在结果页面上使用此功能。分页将自动显示在页面底部
{!! $invoices->render() !!}