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">
                <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">
                    <span class="invalid-tooltip">
                        <strong>{{ $message }}</strong>

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

Enter fullscreen mode Exit fullscreen mode

Image description

In Route

Enter fullscreen mode Exit fullscreen mode

In Controller

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

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

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


Enter fullscreen mode Exit fullscreen mode


Image description


Image description

Image description

after click search button

Image description

Top comments (0)