Search functionality in Laravel

30. April 2016 Intermediate, Laravel 21

Here we ‘ll see about how to implement search functionality in laravel. we ‘ll search the data from the database and show the search results in a table.

Working Demo      Project on Github

Search form :

First lets create a form for search field,

Search action :

When the search button is clicked, it goes to the route search where the logic for fetching data from database is present,

this logic searches the table User for name and email with the input we provide, here we use LIKE operator for searching data,
after fetching the data the data is sent to welcome view, along with the message when no search results are found.

Here all the logic is written in routes file itself, if we want to create a controller we can do it, here only just one simple logic, so we didn’t use controller.

Search results :

Welcome view for showing search results will look like,

The routes file looks like,

for different types of routing check previous post on routing.

Now when someone query the database, we can see the appropriate results.

Working Demo      Project on Github

Look at the screenshots below,

Search functionality in laravel –
Search functionality in laravel –
Search functionality in laravel –
Search functionality in laravel –
Search functionality in laravel –


Working Demo      Project on Github

That’s it folks. We have successfully implemented simple search functionality for fetching data from database in laravel.

Feel free to browse previous posts on custom authentication with validation, routing, and pagination.


21 thoughts on “Search functionality in Laravel”

  • 18
    longkyanh on April 30, 2016 Reply

    return view(‘welcome’)->withDetails($user)->->withQuery ( $q );
    ? should it be only one -> pointer

    • 19
      avinash on April 30, 2016 Reply

      Thanks longkyanh for the check, corrected it now. 🙂

  • 20
    Cris on July 13, 2016 Reply

    I recommend you use the “when” of laravel 5.2 to optimize and refactor this code. regards

    • 21
      avinash on July 13, 2016 Reply

      Thanks Cris for the update,

      I think, i made this search functionality post with laravel 5.1~

  • 22
    Michelle Prather on December 14, 2016 Reply

    Code from last question :

    $q = Input::get ( ‘q’ );
    $trucking_delivery = TruckingDelivery::where(‘ref_no’,’LIKE’,’%’.$q.’%’)->orWhere(‘mawb’,’LIKE’,’%’.$q.’%’)->orWhere(‘hawb’,’LIKE’,’%’.$q.’%’)->get();
    if(count($trucking_delivery) > 0)
    return view(‘search-results’)->withDetails($trucking_delivery)->withQuery ( $q );
    else return view(‘freight-availability’)->withMessage(‘No Details found. Please try your search again !’);

    • 23
      avinash on December 15, 2016 Reply

      You try Session Flash Session::flash ( 'message', 'No Details found. Please try your search again !' );

      and then in you view,

      @if ((Session::has('message')))

      Session::get('message') }}

  • 24
    leicha on January 13, 2017 Reply

    does this work on laravel 5.3.28?

    i dont get any results at all

    but when i delete the lines:

    The Search results for your query {{ $query }} are :

    i get:

    Undefined variable: details

    • 25
      avinash on January 13, 2017 Reply

      It will work on 5.3 too

      That $details variable contains the search results. So a check is made whether results are available are not.

      Make sure the variable $user is getting any results in getting any response.(in routes file here)

  • 26
    Ho Dang KHoa on March 4, 2017 Reply


  • 27
    brunopy on April 24, 2017 Reply

    Where are the methods withDetails and withQuery documented? I’ve been looking through the whole documentation with no success.

    • 28
      avinash on April 24, 2017 Reply

      withDetails and withQuery are not laravel methods they are called magic functions. Which means when we use withSomename in Controller here, we can access them in view as $somename. Take a look at the case of the letters, withCapital in controller —- $capital in view.

      Hope it helps, revert back if not clear.

  • 29
    dbetmDavid on June 11, 2017 Reply

    q, Input does not exists

    • 30
      avinash on June 11, 2017 Reply

      Make sure you have used use Illuminate\Support\Facades\Input; at the top of the controller.

  • 31
    nazarethgroup on June 13, 2017 Reply

    When I click on Search , Its redirecting me to localhost/search.
    Please Help, I downloaded the code through github, and I followed the step, but when I click on Search its redirecting me to localhost/search instead of localhost/searchfunctionality/search.
    I’ve checked the route and it look good
    orWhere ( ’email’, ‘LIKE’, ‘%’ . $q . ‘%’ )->get ();
    if (count ( $user ) > 0)
    return view ( ‘welcome’ )->withDetails ( $user )->withQuery ( $q );
    return view ( ‘welcome’ )->withMessage ( ‘No Details found. Try to search again !’ );
    } );

  • 32
    outa on August 21, 2017 Reply

    When I try this I get error Class ‘App\Posts’ not found

  • 33
    Sanchit Mahajan on October 15, 2017 Reply

    How to write above code in Laravel 5.3.. I am getting error… thank You.

    • 34
      avinash on October 15, 2017 Reply

      What is the error you are getting?

  • 35
    Sanchit Mahajan on October 16, 2017 Reply

    Error in Route function.. How to make this example using controller? thanks…

    • 36
      avinash on October 18, 2017 Reply

      Replace this line Route::any('/search',function(){.......}); with Route::any('/search',YourController@functionName); and place the code in the controller

Leave a Reply