DEV Community

Cover image for How to add simple search to your Laravel blog/website?
Bobby Iliev
Bobby Iliev

Posted on • Originally published at devdojo.com

How to add simple search to your Laravel blog/website?

Introduction

There are many ways of adding search functionality to your Laravel website.

For example, you could use Laravel Scout which is an official Laravel package, you can take a look at this tutorial here on how to install, setup and use Laravel scout with Algolia.

However, in this tutorial here, we will focus on building a very simple search method without the need of installing additional packages.

Prerequisites

Before you begin you need to have Laravel installed.

If you do not have that yet, you can follow the steps on how to do that here or watch this video tutorial on how to deploy your server and install Laravel from scratch.

You would also need to have a model ready that you would like to use. For example, I have a small blog with a posts table and Post model that I would be using.

Controller changes

First, create a controller if you do not have one already. In my case, I will name the controller PostsController as it would be responsible for handling my blog posts.

To create that controller just run the following command:

php artisan make:controller PostsController
Enter fullscreen mode Exit fullscreen mode

Then open your controller with your text editor, and add the following search 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 posts table
    $posts = Post::query()
        ->where('title', 'LIKE', "%{$search}%")
        ->orWhere('body', 'LIKE', "%{$search}%")
        ->get();

    // Return the search view with the resluts compacted
    return view('search', compact('posts'));
}
Enter fullscreen mode Exit fullscreen mode

Note that you would need to change the table names which you would like to search in.

In the example above we are searching in the title and body columns from the posts table.

Route changes

Once our controller is ready, we need to add a new route in the web.php file:

Route::get('/search/', 'PostsController@search')-name('search');
Enter fullscreen mode Exit fullscreen mode

We will just use a standard get request and map it to our search method in the PostsController.

Blade view changes

Now that we have the route and the controller all sorted out, we just need to add a form in our search.blade.php file.

I will use the following simple GET form:

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

Feel free to add this to your existing view and style it accordingly with your classes.

Then to display the results, what you could do is use the following forearch loop in your view:

@if($posts->isNotEmpty())
    @foreach ($posts as $post)
        <div class="post-list">
            <p>{{ $post->title }}</p>
            <img src="{{ $post->image }}">
        </div>
    @endforeach
@else 
    <div>
        <h2>No posts found</h2>
    </div>
@endif
Enter fullscreen mode Exit fullscreen mode

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

Conclusion

This is just one way of building a simple search Laravel!

If you are just getting started with Laravel I would recommend going through this Laravel introduction course.

Hope that this helps!

Top comments (7)

Collapse
 
dima2306 profile image
Dimitri Simonishvili

Indeed, this article is helpful for simple searches. Let me simplify foreach in blade, you just cycle through with forelse instead of checking with condition:

@forelse($posts as $post)
    <div class="post-list">
        <p>{{ $post->title }}</p>
        <img src="{{ $post->image }}">
    </div>
@empty
    <div>
        <h2>No posts found</h2>
    </div>
@endforeach
Enter fullscreen mode Exit fullscreen mode
Collapse
 
bobbyiliev profile image
Bobby Iliev

Yes!! forelse is very handy. I actually go over it in this post here:

How to use Forelse loop in Laravel Blade

Collapse
 
aneesdhansey profile image
Anees Dhansey

You can register a macro in your AppServiceProvider.php and use with the query builder for any table across your whole project.
In the boot method of your AppServiceProvider.php:

Builder::macro('whereLike', function ($attributes, string $searchTerm) {
            $this->where(function (Builder $query) use ($attributes, $searchTerm) {
                foreach (Arr::wrap($attributes) as $attribute) {
                    $query->when(
                        str_contains($attribute, '.'),
                        function (Builder $query) use ($attribute, $searchTerm) {
                            [$relationName, $relationAttribute] = explode('.', $attribute);

                            $query->orWhereHas($relationName, function (Builder $query) use ($relationAttribute, $searchTerm) {
                                $query->where($relationAttribute, 'LIKE', "%{$searchTerm}%");
                            });
                        },
                        function (Builder $query) use ($attribute, $searchTerm) {
                            $query->orWhere($attribute, 'LIKE', "%{$searchTerm}%");
                        }
                    );
                }
            });

            return $this;
        });
Enter fullscreen mode Exit fullscreen mode

Usage:

$users = User::query();
$users = $users->whereLike(['username', 'name', 'email'], $searchText);
Enter fullscreen mode Exit fullscreen mode

Credits:
Freek.Dev

Collapse
 
alexgeorgiev17 profile image
Alex Georgiev

Thanks for sharing it, Bobby!

Collapse
 
bobbyiliev profile image
Bobby Iliev

🙌🙌🙌

Some comments may only be visible to logged-in visitors. Sign in to view all comments.