Vikrant
2 years ago

search not work in laravel 5.4

Posted 2 years ago by Vikrant

here is view :

<nav class="navbar navbar-default">
  <div class="container-fluid">

  <div class="navbar-header">
     <button type="button" class="navbar-toggle collapsed" 
data-toggle="collapse" data target="#bs-example-navbar-collapse-1" aria-        

expanded="false">
    <span class="sr-only">Toggle navigation</span>
    <span class="icon-bar"></span>
    <span class="icon-bar"></span>
    <span class="icon-bar"></span>
  </button>
  <a class="navbar-brand" href="{{ route('product.index') }}">Brand</a>

 </div>

 <div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
   <form class="navbar-form navbar-left" role="search">
     <div class="input-group">
    <input type="text" name="searchname" class="form-control "  id="searchname"     
    placeholder="Search....." style=" margin-left: 200px;  width: 350px; " >
      <div class="input-group-btn">
          <button class="btn btn-default" type="submit">
    <i class="glyphicon glyphicon-search"></i></button>
      </div>
     </div>
    </form>

        <ul class="nav navbar-nav navbar-right">
          <li>
      <a href="{{route('product.shoppingCart')}}">
    <i class="fa fa-shopping-cart" aria-hidden="true"></i> 
         Shopping Cart
         <span class="badge">{{ Session::has('cart') ? Session::get('cart')->totalQty : '' }}</span>
      </a>
    </li>
      <li class="dropdown">
      <a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" 
aria-haspopup="true" aria-expanded="false">
<i class="fa fa-user-circle" aria-hidden="true"></i> User Managment
<span class="caret">        </span></a>
      <ul class="dropdown-menu">
        @if(Auth::check())

        <li><a href="{{ route('user.profile')}}">User Profile</a></li>
        <li role="separator" class="divider"></li>

        <li><a href="
        {{route('user.logout')}}">Logout</a></li>
        @else
         <li><a href="{{ route('user.signup')}}">Signup</a></li>
        <li><a href="{{route('user.signin')}}">Signin</a></li>
        @endif
       
        
         </ul>
     </li>
     </ul>
  </div><!-- /.navbar-collapse -->
  </div><!-- /.container-fluid -->
</nav>

    @section('scripts')
<script type="text/javascript">
 $('#searchname').autocomplete({


 source : '{!!URL::route('autocomplete')!!}',
  minlenght:1,
  autoFocus:true,
  select:function(e,ui){
  
  // $('#id').val(ui.item.id);
  // $('#name').val(ui.item.value);

  }
});
</script>
@endsection

here is model :

    class Product extends Model
    {
     public $table = 'products';  

     protected $fillable = ['imagePath','title','description','price'];
    }

here is controller :

    public function autocomplete(Request $request)
       {
         $term = $request->term; //jquery
          $data = Product::where('title','LIKE', strtolower("$term%"))->take(5)->get();
         $result = array();
 
         foreach ($data as $key => $value) 
            {
         $result[] = ['id'=>$value->id,'value'=>$value->title]; 
         }
            return response()->json($result);
     }

here is route :

Route::get('/autocomplete',array
('as'=>'autocomplete','uses'=>'[email protected]'));

here is javascript(public/js/search.js) :

    $('#searchname').autocomplete({
    source : '{!!URL::route('autocomplete')!!}',
    minlenght:1,
    autoFocus:true,
    select:function(e,ui){
        
    //$('#id').val(ui.item.id);
    //$('#name').val(ui.item.value);
    }
    });

Please sign in or create an account to participate in this conversation.