搜索不在laravel 5.1中工作

时间:2016-08-21 06:38:03

标签: php mysql laravel search laravel-5.1

我目前正在做一个项目。在这个项目中我无法使用我的搜索表单。我的搜索按钮没有显示任何值。它刚刚返回一个空页面。请帮我。我的路线是

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

我的刀片模板是

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

    }

3 个答案:

答案 0 :(得分:0)

首先,在路线中你可以像使用post方法一样编写传递搜索关键字

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

之后,在刀片模板中对刀片模板进行一些修改

    {!! 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();
     }

}

确保你指向正确的视野路径,欢呼伴侣肯定会对你有用:)

答案 1 :(得分:0)

您正在寻找$req->input('name'),但您已在表单中将其称为search; name="search"

将输入更改为:

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

或您的控制器请求:

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

您的表单看起来似乎缺少提交按钮?

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

答案 2 :(得分:0)

问题的解决方案是

function WebkitToEpoch() {
    var wk = document.we.wk.value;
    var sec = Math.round(wk / 1000000);
    sec -= 11644473600;
    var datum = new Date(sec * 1000);
    var outputtext = "<b>Epoch/Unix time</b>: " + sec;
    outputtext += "<br/><b>GMT</b>: " + datum.toGMTString() + "<br/><b>Your time zone</b>: " + datum.toLocaleString();
    $('#resultle1').html(outputtext);
}