Jahid26 Jahid26 - 3 months ago 7
MySQL Question

Search not working in laravel 5.1

I am currently doing a project. In this project I cant do my search form working.My search button not showing any value. It just returned an empty page. Please Help Me. My Route is

Route::get('/search', array('as' =>'search' ,'uses' => 'UserController@search'));


My Blade Template is

<header id="header">
<div class="top-bar">
<div class="container">
<div class="row">
<div class="col-sm-12 col-xs-12">
<div class="social">
<ul class="social-share">
<li><a href="#"><i class="fa fa-google-plus"></i></a></li>
<li><a href="#"><i class="fa fa-facebook"></i></a></li>
<li><a href="#"><i class="fa fa-twitter"></i></a></li>
<li><a href="#"><i class="fa fa-skype"></i></a></li>
</ul>
<div class="search">
{!! Form::open(['route' => 'search', 'method'=> 'GET']) !!}

<input type="text" class="search-form" name="search" autocomplete="off" placeholder="Search">

<i class="fa fa-search"></i>

{!! Form::close() !!}
</div>
</div>
</div>
</div>
</div>
</div><!--/.container-->
</div><!--/.top-bar-->

<nav class="navbar navbar-inverse" role="banner">
<div class="container">
<div class="navbar-header">
<button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-collapse">
<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="{!! URL::route('home') !!}">{!! Html::image('images/logo.png','logo') !!}</a>
</div>

<div class="collapse navbar-collapse navbar-right">
<ul class="nav navbar-nav">
<li <?php if($active == 'home') {echo 'class="active"';} ?>><a href="{!! URL::route('home') !!}">Home</a></li>

<li <?php if($active == 'about_us') {echo 'class="active"';} ?>><a href="{!! URL::route('about_us') !!}">About Us</a></li>

<li <?php if($active == 'hospital') {echo 'class="active"';} ?> class="dropdown" >
<a href="#" class="dropdown-toggle" data-toggle="dropdown">Hospitals <i class="fa fa-angle-down"></i></a>
<ul class="dropdown-menu">
@foreach($divisions as $division)

<li><a href="{!! URL::route('district', $division['id']) !!}">{{ $division['name'] }}</a></li>

@endforeach

</ul>
</li>

<li <?php if($active == 'doctor') {echo 'class="active"';} ?> class="dropdown" >
<a href="#" class="dropdown-toggle" data-toggle="dropdown">Doctors <i class="fa fa-angle-down"></i></a>
<ul class="dropdown-menu">

@foreach($divisions as $division)
<li><a href="{!! URL::route('districts',$division['id']) !!}">{{ $division->name }}</a></li>

@endforeach

</ul>
</li>

<li <?php if($active == 'contact') {echo 'class="active"';} ?>><a href="{!! URL::route('contact') !!}">Contact Us</a></li>

<li <?php if($active == 'login') {echo 'class="active"';} ?>><a href="{!! URL::route('login') !!}">Login</a></li>

<li <?php if($active == 'register') {echo 'class="active"';} ?>><a href="{!! URL::route('register') !!}">Sign Up</a></li>
</ul>
</div>
</div><!--/.container-->
</nav><!--/nav-->

</header><!--/header-->


My Controller is

public function search(Request $req)
{

$divisions = Division::all();
$doctors = Doctor::orderBy('name');
$name = $req->input('name');
if(!empty($name)) {
$doctors->where('name', 'LIKE', '%' . $name . '%');
}
$doctors=$doctors->paginate(1);
return view('users.index')
->with('divisions',$divisions)
->with('doctors',$doctors);

}

Answer

The Solution of the problem is

public function search(Request $req)
    {

   $divisions = Division::all();
   $name = $req->input('search');

   $doctors = User::where('name', 'LIKE', '%' . $name . '%')->get();

   return view('users.search')
               ->with('divisions',$divisions)
                ->with('doctors',$doctors);

     }