Implement search in laravel

implement-search-with-laravel

Hello World, Welcome to my website. Today i am going to implement search in laravel. In this tutorial, I will extends my previous tutorial. My previous four laravel tutorials are interconnected with each other. I want to share my knowledge with example so that i am following my previous tutorial because in my previous tutorials i an working on blog application. In this tutorial, we will implement search with url parameter and count the total search result.

I have divided this tutorial into following parts,

  • Create search form
  • Implement search with controller
  • Run the project


Create search form

In this step, we will create search form template our post list template file.

  • Open /resources/views/front.blade.php
  • Replace the container div code with following code.
    <div class="container mt-4">
        <div class="row">
            <div class="col-12 col-sm-4">
                <p>Click the button below to fetch the data</p>
                <button class="fetch-data btn btn-primary">Fetch Data</button>
            </div>
            {{-- Search Start --}}
            <div class="col-12 col-sm-8">
                <form method="get">
                    <input type="text" name="q" class="form-control" placeholder="Enter Here" />
                    <input type="submit" value="Search" class="btn btn-primary mt-2" />
                </form>
            </div>
        </div>
        {{-- Search End --}}
        <div class="ajax-data mt-4">
            @if(isset($searchedData))
                @if(count($searchedData)>0)
                    <p class="bg-light p-2 border"><strong class="text-success">({{ count($searchedData) }})</strong> Data Found for <strong class="text-danger">{{ $q }}</strong></p>
                    @foreach($searchedData as $data)
                    <div class="border p-3 mb-4 shadow-sm">
                        <h3 class="border-bottom pb-2"><a href="{{ url('post-detail/'.$data->id) }}">{{ $data->title }}</a></h3>
                        <p>{{ $data->body }}</p>
                    </div>
                    @endforeach
                @else
                    <p class="bg-light p-2 border"><strong class="text-success">(0)</strong> Data Found for <strong class="text-danger">{{ $q }}</strong></p>
                @endif
            @endif
        </div>
    </div>

Implement search with controller

In this step, we will get the search value from url and find that value in database with like query.

  • Open app/Http/Controllers/PostController and replace the index method with following code.
function index(Request $request){
        $q=Input::get('q');
        if(!empty($q)){
            $posts=Post::where('title','like','%'.$q.'%')->get();
            return view('front',['searchedData'=>$posts,'q'=>$q]);
        }else{
            return view('front');
        }
    }

Please add the following code in PostController before class

use Illuminate\Support\Facades\Input;

Run the project

  • Open project in the browser
  • Enter some string in search form and submit
  • You will see the related result with data counting

Leave a Reply