Simple Search In Laravel for Blogs/Articles

Search option in any website increases the user interaction and also helps to find desired content with ease. There are may ways to implement search in Laravel application and also there are great packages like Laravel Scout . But in this tutorial we will see the basic and the simplest way to search items in our database in Laravel.

Before Starting

Before starting the tutorial make sure you have Laravel Installed and set up if not click Here to install Laravel in your machine and also you have setup your Controller to index/show posts.

Lets say you have Articles Model, migrations, Controller and Index. Now lets move on to our goal.

Step 1 : Add search bar/box in your page.

Lets create a search bar in our blade file. Its always best practice to add search bar in our header/Navigation Section. Here is the sample search form.

<form action="{{ route('search') }}" method="GET">
    <input type="text" name="search" required/>
    <button type="submit">Search</button>
</form> 

You can customize the styles as your need.

Step 2 : Register search route

To register the route open web.php file and add this line. (Laravel 8 and higher convention)

Route::get('/search/', [ArticleController::class, 'search'])->name('search'); 

Make sure the route name matches with the form action route. And also don't forget to use the ArticleController in top.

Note: You can free to use separate controller for search jus replace Article with your controller name

Step 3 : Add search method in controller

Lets open our controller (Whatever controller you are using). create a public method caller search().

Add the following lines of code inside the method.

public function search(Request $request){
    // Get the search value from the request
    $search = $request->input('search');

    // Search in the title and body columns from the articles table
    $articles = Article::query()
        ->where('title', 'LIKE', "%{$search}%")
        ->orWhere('body', 'LIKE', "%{$search}%")
        ->get();

    // Return the search view with the resluts compacted
    return view('search', compact('articles'));
} 

In above code change the column names to match with your articles table. Or whatever table you have.

If you are using different Controller i.e. other than the ArticleController then make sure to import Article model in the top use section.

Step 4 : Showing the search result

To show the search let's create new blade file search.blade.php and add the following code.

<div>
    @forelse($artilces as $article)
        	<div class="articles-list">
            	<h2>{{ $article->title }}</h2>
            	<p>
                    {{ $article->body }}
                </p> 
        	</div>
    @empty 
    	<div>
        	<h2>No Article found</h2>
    	</div>
    @endforelse
</div> 

The above would display your article if there are any found and would print No Article found message if there are none.

You are always free to customize the style. I have return the view here but you can you modal pop-up to display.

Conclusion

Above explained method is just the basic and simplest way to implement search functionality in your Laravel web application. We have used LIKE syntax to match our query. Frankly speaking this is not the best way to do this. But the basic and easiest one.

Hope you have learned something useful today. Thanks for reading.

Comments :

  • Mandip Shahee 5 months ago

    Hello...