Debug School

rakesh kumar
rakesh kumar

Posted on

How to search country,state and city using single input field with example

To search for a country name, state name, or city name using a single input field in Laravel, you can use a query that combines multiple conditions. Here's an example of how you can implement this.

In blade file

 <div class="col-sm-4 mb-3 mb-sm-0">
                <div class="card  mt-2">
                <h1 style="margin-top:10px;">Find Business Broker</h1>               
                <div style="background-color:#f89d13;" class="card-body mt-4">
      <form method="POST" action="{{ route('page.searchlocbroker') }}" id="searchForm">
                @csrf
                <div class="form-row mb-3">
      <div class="col-md-8 mt-2">    
                <input type="text" class="form-control" autocomplete="off" name="cityinput" id="cityinput" required placeholder="Enter a country or state or city">
                    @error('city_id')
                    <span class="invalid-tooltip">
                        <strong>{{ $message }}</strong>
                    </span>
                    @enderror
                </div>


                <div class="col-md-4 mt-4">
                    <button class='view-detail-button'  type="submit">Search</button>                   
                    <div id="searchResults"></div>
                    </div>

                    </div>
                    </form>
                    </div>
Enter fullscreen mode Exit fullscreen mode

Image description

In Route

Route::post('/searchlocbroker','PagesController@searchlocbroker')->name('page.searchlocbroker');
Enter fullscreen mode Exit fullscreen mode

In Controller

    public function searchlocbroker (Request $request)     
    {
    log::info("searchbroker bheetar aayega naaa");
    log::info("searchbroker");
    log::info($request); 

    $searchTerm = $request->input('cityinput');
    $all_country = DB::table('countries')->get();
    $searchbroker =  DB::table('broker')
        ->leftJoin('company', 'broker.company_id', '=', 'company.id')
        ->leftJoin('cities', 'broker.city_id', '=', 'cities.id')
        ->leftJoin('states', 'broker.state_id', '=', 'states.id')
        ->leftJoin('countries', 'broker.country_id', '=', 'countries.id')
        ->where('countries.country_name', 'LIKE', '%' . $searchTerm . '%')
        ->orWhere('states.state_name', 'LIKE', '%' . $searchTerm . '%')
        ->orWhere('cities.city_name', 'LIKE', '%' . $searchTerm . '%')
        ->select('broker.*','company.logo','company.name','countries.country_name','states.state_name','cities.city_name')
        ->get();

        if ($searchbroker->isEmpty()) {
            return view('frontend.foundsnot', compact('all_country','searchTerm')); 
           }
           else{
           return view('frontend.searchbroker', compact('searchbroker','all_country','searchTerm'));  

        }

    }
Enter fullscreen mode Exit fullscreen mode

======================or================================

Image description

Output

Image description

Image description

after click search button

Image description

Top comments (0)