Pagination doesn't work after perform search

2019-09-05 00:43发布

I have a form in Laravel. I used pagination in this form and search option too. Pagination works fine when I didn't search anything. If I search something which has records more than 10 (my pagination record limit is 10), it shows result perfectly in first page but in second page or third page it shows the records of whole database. It's ignore the search criteria.

Controller Code:

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'));
}

After performing search my URL look like this:

http://my-site.com/invoice?invoiceNumber=&client=&startDate=2016-01-01&endDate=2016-02-08

After this search when I click on page 2, the URL become this:

http://my-site.com/invoice?page=2

3条回答
Melony?
2楼-- · 2019-09-05 01:29

In your view file, the render has to be like given below:

{!! $invoices->appends(Input::except('page'))->render() !!}
查看更多
3楼-- · 2019-09-05 01:29

Try to use this bellow code (change as per your requirement)

return view('invoice.index')->withInvoices($invoices->appends(Input::except('page'));

Hope it's working for you...

查看更多
别忘想泡老子
4楼-- · 2019-09-05 01:44

Simply use this on the result page.Pagination will be automatically visible at the bottom of the page

{!! $invoices->render() !!}
查看更多
登录 后发表回答