Berwyn's avatar

Pagination Link Breaks after Applying a Filter?

I have an issue after applying filters on my records. Essentially, once the filtering process is done the pagination links break and try to point to a page that doesn't exist.

I'll try to give you the code step by step, let's start with the route:

Route::post('/filter-leads', 'FiltersController@leadsFilter');

That gets sent all the filter criteria by AJAX. Next up is the controller:

public function leadsFilter(Request $request) {
        //return response()->json($request); //value checker pre-validation
        $val = $request->validate([
            'startDate' => 'nullable|date',
            'endDate' => 'nullable|date',
            'leadTypes.*' => 'nullable|string',
        ]);

        //return response()->json($val); //value checker post-validation
        //default static variables
        $company = Company::where('id', auth()->user()->company_id)->first();
        $status = LeadStatusOption::where('company_id', auth()->user()->company_id)->get();

        $query = Lead::query()->where('company_id', $company->id);

        //if catchers and query scalers
        if($request->filled(['startDate', 'endDate'])) {
            $startDate = Carbon::createFromFormat('Y-m-d', $val['startDate'])->format('d/m/Y'); //carbon usage needed due to the format the model is expecting, this then gets converted by the scope
            $endDate =  Carbon::createFromFormat('Y-m-d', $val['endDate'])->format('d/m/Y');
            $query = $query->StartDate($startDate)->EndDate($endDate);
        }
        else if ($request->filled('startDate')){
            $startDate = Carbon::createFromFormat('Y-m-d', $val['startDate'])->format('d/m/Y');
            $query = $query->StartDate($startDate);
        }
        else if ($request->filled('endDate')){
            $endDate =  Carbon::createFromFormat('Y-m-d', $val['endDate'])->format('d/m/Y');
            $query = $query->EndDate($endDate);
        }
        if($request->filled('leadTypes')) {
            $query = $query->LeadTypes($val['leadTypes']);
        }

        //final query builder

        $leads = $query->paginate(15);


        return view('partials.leadTable')
            ->with([
                'leads' => $leads,
                'company' => $company,
                'status' => $status,
            ]);
    }

After that I just use the {{$leads->links()}} on the bottom of my page. First of all, this is what the url looks like on the base page with working pagination: http://demo:8888/test-module/leads?page=1

Then after filtering this is what it gives me: http://demo:8888/test-module/filter-leads?page=2 with the following error message:

InvalidArgumentException View [show] not found. http://demo:8888/test-module/filter-leads?page=2 Hide solutions show was not found. Did you mean home?

I'd appreciate any help with this!

Thank you in advance!

0 likes
3 replies
jlrdw's avatar

You have to append parameters to query string, example:

In controller

$params = array('psch' => $dogsearch, 'aval' => $aval);

Pass $params to view also.

view

{{ $dogs->appends($params)->links() }}

https://laravel.com/docs/8.x/pagination#introduction

Scroll to Appending To Pagination Links

Berwyn's avatar

Hey @jlrdw thank you for your response, I have read about what you just sent. However, to me it didn't make much sense, and still doesn't. I will play around with what I'e got and post any updates, but as of yet I don't even know here to declare the:

$params = array('psch' => $dogsearch, 'aval' => $aval);

Still, I will give it a go and come back if I figured something out. Cheers!

Berwyn's avatar

Okay, so my bad, I had another little bug I fixed now that prevented me last time to use the following:

{{$leads->withQueryString()->links()}}

The above now works however it just gives me table on its own without any of the markup from the master page. I'll update you again once I fixed that.

Please or to participate in this conversation.