搜索在 laravel 5.1 中不起作用

Search not working in laravel 5.1

我目前正在做一个项目。在这个项目中,我无法执行搜索表单 working.My 搜索按钮未显示任何值。它只是返回一个空白页面。请帮我。我的路线是

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

我的 Blade 模板是

<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-->

我的控制器是

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);

    }

首先在路由中你可以这样写使用post传递搜索关键字的方法

    Route::post('/search', 'UserController@search_bar')->name('search');

然后在你的 blade 模板中对 blade 模板做一点修改

    {!! Form::open(['route' => 'search']) !!}

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

   <i class="fa fa-search"></i>  {!! Form::submit($btnTxt,array('class'=>'btn btn-success')) !!} {!! Form::close() !!}

现在在你的控制器中试试我的,因为你没有提到你正在搜索哪个模型,所以我只向你展示一种方法,你可以抓住它并自己尝试,

    public function search(Request $request)
    {
    $name = $request->input('search');
    if(null !== $name)
    {
         $doctors = App\Doctor::where('name', 'LIKE', '%' . $name .'%')->paginate(10);
  return view('users.index',compact('doctors'));
    }else{
      return redirect()->back();
     }

}

确保您指向正确的视图路径,干杯伙计它肯定对您有用:)

您正在寻找 $req->input('name') 但您在表格中将其命名为 searchname="search".

将您的输入更改为:

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

或者您的控制器请求:

$name = $req->input('search');

您的表单似乎也缺少提交按钮?

<input type="submit" value="Search">

问题的解决方法是

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);

     }