2016-08-21 169 views
0

我目前正在做一個項目。在這個項目中,我不能做我的搜索表單working.My搜索按鈕沒有顯示任何價值。它只是返回一個空白頁面。請幫幫我。我的路線是搜索不工作laravel 5.1

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

我的刀片模板是

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

    } 

回答

0

問題的解決方案是

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

    } 
0

首先的路線,你可以把它寫這樣的後期使用方法通過搜索關鍵字

Route::post('/search', '[email protected]_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(); 
    } 

}

確保您正在指向正確的視覺路徑,歡呼隊友它會爲你工作肯定:)

+0

對不起你的代碼不起作用。它沒有顯示任何信息@Roshan Ranabhat –

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"> 
+0

它不顯示任何搜索信息。 Plz heelp –

+0

@ Jahid26你嘗試過我建議的嗎? – James

+0

是的,我嘗試了你的代碼@James –