How to implement search functionality in laravel 8 and laravel 7 downwards
Kingsconsult
Posted on September 29, 2020
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, in case you just need the code, you can get it from the GitHub repo.
Click on my profile to follow me to get more updates.
Step 1: Modify the index method in the project controller
- Add Request $request as a parameter to the Index method
- Edit the query to get all the projects
- 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
- An input tag (where the user will enter the text to search).
- A submit button (This will trigger the search functionality after the text has been added).
- Another button to refresh our search result. ```
<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
💖 💪 🙅 🚩
Kingsconsult
Posted on September 29, 2020
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.
Related
laravel How to implement search functionality in laravel 8 and laravel 7 downwards
September 29, 2020