Search functionality in Laravel

30. April 2016 Intermediate, Laravel 16
Search functionality in Laravel

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 - jusLaravel.com
Search functionality in laravel – jusLaravel.com
Search functionality in laravel - jusLaravel.com
Search functionality in laravel – jusLaravel.com
Search functionality in laravel - jusLaravel.com
Search functionality in laravel – jusLaravel.com
Search functionality in laravel - jusLaravel.com
Search functionality in laravel – jusLaravel.com
Search functionality in laravel - jusLaravel.com
Search functionality in laravel – jusLaravel.com

 

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.

 



16 thoughts on “Search functionality in Laravel”

  • 1
    longkyanh on April 30, 2016 Reply

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

    • 2
      avinash on April 30, 2016 Reply

      Thanks longkyanh for the check, corrected it now. 🙂

  • 3
    Cris on July 13, 2016 Reply

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

    • 4
      avinash on July 13, 2016 Reply

      Thanks Cris for the update,

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

  • 5
    Michelle Prather on December 14, 2016 Reply

    Code from last question :

    //routes.php
    Route::any(‘/search’,function(){
    $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 !’);
    });

    • 6
      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') }}

  • 7
    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:

    @if(isset($details))
    The Search results for your query {{ $query }} are :

    i get:

    Undefined variable: details

    • 8
      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)

  • 9
    Ho Dang KHoa on March 4, 2017 Reply

    thank

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

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

  • 12
    dbetmDavid on June 11, 2017 Reply

    q, Input does not exists

    • 13
      avinash on June 11, 2017 Reply

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

  • 14
    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
    PLEASE HELP
    orWhere ( ’email’, ‘LIKE’, ‘%’ . $q . ‘%’ )->get ();
    if (count ( $user ) > 0)
    return view ( ‘welcome’ )->withDetails ( $user )->withQuery ( $q );
    else
    return view ( ‘welcome’ )->withMessage ( ‘No Details found. Try to search again !’ );
    } );

Leave a Reply