How to implement search functionality in laravel 8 and laravel 7 downwards
Tags: laravel, php, webdev, html
Published: 2020-09-29 11:23:45

Today, I am going to drop a simple hint, how to implement search functionality in your crud app, this will allow you to just get a specific item or items that have a similar name from the search result in a list of hundreds or even thousands of data from the database. 
I am going to modify our index method from our previous app [Laravel 8 CRUD](https://dev.to/kingsconsult/laravel-8-crud-bi9), in case you just need the code, you can get it from the [GitHub repo](https://github.com/Kingsconsult/laravel_8_crud).

Click on my [profile](https://dev.to/kingsconsult) to follow me to get more updates.

### **Step 1: Modify the index method in the project controller**
1. Add Request $request as a parameter to the Index method
2. Edit the query to get all the projects
![index method in controller](https://res.cloudinary.com/kingsconsult/image/upload/v1601378044/laravel%208%20search%20functionality/1_curujv.png)
    - In the first parameter for our “where” clause, we are going to only query the projects that the name is not null.
    - In the second parameter, we are going to query the projects that the request is bringing, in our case where the request is similar to the name of the project.
    - You can choose to order how your search results show up, I order mine according to the descending order of id.

### **Step 2: add the form that will send the request to the controller in index.blade.php**
I added the following
1. An input tag (where the user will enter the text to search).
2. A submit button (This will trigger the search functionality after the text has been added).
3. Another button to refresh our search result.
```
   <div>
        <div class="mx-auto pull-right">
            <div class="">
                <form action="{{ route('projects.index') }}" method="GET" role="search">

                    <div class="input-group">
                        <span class="input-group-btn mr-5 mt-1">
                            <button class="btn btn-info" type="submit" title="Search projects">
                                <span class="fas fa-search"></span>
                            </button>
                        </span>
                        <input type="text" class="form-control mr-2" name="term" placeholder="Search projects" id="term">
                        <a href="{{ route('projects.index') }}" class=" mt-1">
                            <span class="input-group-btn">
                                <button class="btn btn-danger" type="button" title="Refresh page">
                                    <span class="fas fa-sync-alt"></span>
                                </button>
                            </span>
                        </a>
                    </div>
                </form>
            </div>
        </div>
    </div>
```
That is all, this is our result below
**all the projects in the database**
![complete projects](https://res.cloudinary.com/kingsconsult/image/upload/v1601378044/laravel%208%20search%20functionality/2_lxfhey.png)
**only projects with laravel in the name**
![only projects with laravel](https://res.cloudinary.com/kingsconsult/image/upload/v1601378044/laravel%208%20search%20functionality/3_vnhxwh.png)
**only projects with mana in the name**
![only projects with mana](https://res.cloudinary.com/kingsconsult/image/upload/v1601378044/laravel%208%20search%20functionality/4_n2x9qg.png)
**only projects with 1 in the name**
![only projects with 1](https://res.cloudinary.com/kingsconsult/image/upload/v1601378044/laravel%208%20search%20functionality/5_tntdvx.png)


as usual, You can follow me, leave a comment below, suggestion, reaction, or email me. 
Click on [my profile](https://dev.to/kingsconsult), to see my other post and contacts