Pagination doesn't work after perform search

2019-09-05 01:34发布

问题:

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

回答1:

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

{!! $invoices->appends(Input::except('page'))->render() !!}


回答2:

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...



回答3:

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

{!! $invoices->render() !!}