Paginated data with Search functionality – laravel

Paginated data with Search functionality – laravel

Hello everyone! Welcome to In previous posts, we saw about pagination and search functionality individually. Here we ‘ll combine search functionality with paginated data results.

You can also watch the video on YouTube here.

Working Demo      Project on Github

It’s quite common to include search option and pagination functionality for displaying any type of large data. So instead of using some table extenders which fetch all the records at a time, it’s better to use built-in paginators which only gets the required number of records each time.

Here in this elegant framework, Laravel, its quite easy to implement these functionalities with very few lines of code.

First, we ‘ll show chunks of dummy data with about 1000 rows and limiting to only 25 rows per page. This dummy data is collected from

Route::get ( '/', function () {
    $dummyDetails = User::paginate(25);
    return view ( 'welcome' )->withUsers($dummyDetails);
} );

for reference, you can find the dummy details SQL file here

Pagination View :

Now in the view file welcome.blade.php, we show this data limiting to only 25 results per page.

<div class="container">
    <h2>Sample User details</h2>
        <table class="table table-striped">
                @foreach($users as $dummy)

        {!! $users->render() !!}@endif
Search functionality with paginated data -
Search functionality with paginated data –

Search Form :

Now we ‘ll use the form for search field which we created in the previous post on Search functionality.

<div class="container">
        <form action="/search" method="POST" role="search">
            {{ csrf_field() }}
            <div class="input-group">
                <input type="text" class="form-control" name="q"
                    placeholder="Search users"> <span class="input-group-btn">
                    <button type="submit" class="btn btn-default">
                        <span class="glyphicon glyphicon-search"></span>

Pagination and Search logic :

The logic for a handling search is,

Route::any ( '/search', function () {
    $q = Input::get ( 'q' );
    if($q != ""){
    $user = User::where ( 'name', 'LIKE', '%' . $q . '%' )->orWhere ( 'email', 'LIKE', '%' . $q . '%' )->paginate (5)->setPath ( '' );
    $pagination = $user->appends ( array (
                'q' => Input::get ( 'q' ) 
        ) );
    if (count ( $user ) > 0)
        return view ( 'welcome' )->withDetails ( $user )->withQuery ( $q );
        return view ( 'welcome' )->withMessage ( 'No Details found. Try to search again !' );
} );

Working Demo      Project on Github

Here we used HTTP method any() Route::any() but in the previous post on search we used HTTP method post() Route::post(). The difference is in the previous post we just fetch data from the database and show the result, as we are interacting with database we should use post method, whereas here we first fetch the all the data(all 1000 records) so post method and then we show only 5 results(paginated value here is 5)of them, so when we click next button of the pagination then we should get the already obtained results, so here get method is used. As we are using both post and get methods. We write the route as Route::any()

Search functionality with paginated data -
Search functionality with paginated data –

Here while showing search results we limited data to only 5 results per page.


Working Demo      Project on Github

That’s it, folks, we ‘ve successfully implemented search functionality with paginated data. Make sure you ‘ve gone through previous posts on search and pagination.

10 thoughts on “Paginated data with Search functionality – laravel”

  • 1
    123 on April 5, 2017 Reply


    • 2
      avinash on April 6, 2017 Reply

      For more such tutorials follow

  • 3
    jemirisselvi on August 4, 2017 Reply


  • 4
    Ali on May 23, 2018 Reply

    hello, I did everything u did in this video but im getting a ‘Whoops, looks like something went wrong’ error page when i click on the next page pagination button

    • 5
      avinash on May 23, 2018 Reply

      That is a general error. Change Debug to true in config/app.php for more specific error. So I can help you.
      `’debug’ => env(‘APP_DEBUG’, true),`

  • 6
    123456 on October 9, 2018 Reply

    your tutorials solve many problem my project..alot of thanx..

    • 7
      avinash on October 11, 2018 Reply

      Am very glad, these helped you, Thanks for your feedback.
      Keep following for more such tutorials.

Leave a Reply