Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

JuD3's avatar
Level 1

Ajax Update Existing Pagination

Hi Community Fairly new to Laravel and wanted to know how I would update Laravels Pagination in Javascript. If that is at all possible and how to go about it

0 likes
4 replies
Abi's avatar

@jud3

Pagination works by sending a page params to the page.

let's say you have a posts page, when you visit domain.com/posts it will get the results as if you hit domain.com/posts?page=1 , then if you hit omain.com/posts?page=2, it will get the next set of results so on and so forth.

If you wanna do it with ajax the only thing you need to do is hit those urls.

I have created a jQuery plugin for laravel pagination. if you wanna try it or just look at the code so you can see how it works.

https://github.com/abisalazar1/laravel-content-loader

JuD3's avatar
Level 1

@Abi

Thanks for your swift response. I took a look at it and I was a bit confused with it maybe how I would implement it in my code or maybe I do not fully understand pagination but if you will bear with me, here is a little snippet of my code so you could understand what I mean and maybe help me move forward:

My Route:

Route::get('/vehicles/search/{make}/{model}/{type}/{yearf}/{yeart}/{minp}/{maxp}', 'VehiclesController@searchVehicle');

My controller:

    public function searchVehicle($make,$model,$type,$yearf,$yeart,$minp,$maxp){
        $results = Vehicles::where('make', 'LIKE', '%'.$make. '%')->orWhere('model','LIKE', '%'.$model.'%')->paginate(2);

return($results);

}

Javascript:

       var strUrl = vehicle_url + "search/" + vmake + "/" + vmodel + "/" + yearfrom + "/" + yearto + "/" + type + "/" + minprice + "/" + maxprice;
        // prompt("test", strUrl);
        var obj = sendRequest(strUrl);

So in the html it is the variable name -> links(); and would like to know how to update that in the JS

Abi's avatar
Abi
Best Answer
Level 24

@JuD3 I see so you want a pagination/filter page. I think (I'm not sure) if you set the the requirements in your route, the user must set those variables in that order as well. so they won't be able to do something like domain.com/search/model/maxprice as the second params is not price.

What you want is just to hit 1 single url domain.com/search and then send different params like domain.com/search?page=1&model=bmw&minprice=12000 so they have more flexibility and if they don't have any params just paginate all cars.

let's do this first.

///Change your route to


Route::get('/search', 'VehiclesController@searchVehicle')->name('CarSearch');


/// your controller

use Illuminate\Http\Request;

public function searchVehicle(Request $request) {

        $results = (new Vehicles())->newQuery();

        if($request->make){
            $results->where('make', 'LIKE', '%'.$request->make. '%');
        }

        if($request->model){
            $results->where('model','LIKE', '%'.$request->model.'%');
        }


        return view('your view here')->withResults($results->paginate(10));
    }

// blade file

   <ul id="">
                @foreach($results as $result)
                    <li>{{$result->name}}</li>
                @endforeach
                </ul>

                {{ $results->appends(\Request::all()) }}


once you have done that we do the javascript. "this is not perfect but it will get you started"

JuD3's avatar
Level 1

Great! @Abi Thanks, and yes it's more about filtering the page. I have successfully done that.

Please or to participate in this conversation.