2017-08-14 49 views
-1

我嘗試laravel本教程的Live搜索Laravel路線的搜索

但它在homepage(index)

我想它訪問localhost/laravel/public/search

這裏是控制器

class SearchController extends Controller 
{ 
public function index() 
{ 
return view('search.search'); 
} 

public function search(Request $request) 
{ 
    if ($request->ajax()) 
     $output =""; 
     $orderinfo=DB::table('tb_order')->where('shipcustomername','LIKE','%' . $request->search.'%') 
             ->orWhere('orderId','LIKE','%' .$request->search. '%')->get(); 

    if ($orderinfo) 
    { 

     foreach ($orderinfo as $key =>$orderinfo){ 


     $output.='<tr>' . 
       '<td>' .$orderinfo->orderId .'</td>' . 
       '<td>' .$orderinfo->email .'</td>' . 
       '<td>' .$orderinfo->subsource .'</td>' . 

       '</tr>'; 

     } 

     return Response($output); 

    } 

和我的路線

Route::get('/' ,'[email protected]'); 
Route::get('/search' ,'[email protected]'); 

在我的資源文件夾 我有文件夾,搜索,它是包含search.blade.php

<div class="container"> 
     <div class="row"> 
     <div class="panel panel-default"> 
      <div class="panel-heading"> 
       <h3>Order Info</h3>    
      </div> 
      <div class="panel-body"> 
       <div class="form-group"> 
        <input type="text" class="form-control" id="search" name="search"></input>     
       </div> 
       <table class="table table-bordered table-hover "> 
        <thead> 
         <tr> 
          <th>OrderID</th> 
          <th>Email</th> 
          <th>SubSource</th> 
         </tr> 
        </thead> 
        <tbody> 

        </tbody> 
        </table>  
       </div> 
     </div> 
    </div> 
     <script type="text/javascript"> 
      $('#search').on('keyup',function(){ 
       $value=$(this).val(); 
       $.ajax({ 

        type : 'get', 
        url : '{{URL::to('search')}}', 
        data : {'search':$value}, 
        success:function(data){ 
         $('tbody').html(data);     
        } 

       }); 
      }); 
     </script> 

</body> 

我知道這是對指數的路線,

Route::get('/' ,'[email protected]'); 

但如果試圖改變這種到

Route::get('search' ,'[email protected]'); 

我得到錯誤500

什麼是正確的方式來路由這個,所以它不會使用索引

謝謝

+0

嘗試更改'路線:: get('search','SearchController @ index');'到'Route :: get('/ search', 'SearchController @ index');' –

+0

請添加搜索刀片 –

+0

@Leo_Kelmendi我也有search.blade.php查看文件夾 –

回答

1

有您要發送空數據的好機會,試圖改變這一點:

$value=$(this).val(); 

爲此:

var value = $('#search').val(); 

如果沒有這則你也沒有提交數據,以及添加的形式:

{{ Form::open(array('method'=>'GET','class'=> 'col-md-6','url' => '/search', 'id'=>'searchform')) }} 

<div class="form-group"> 
    <input type="text" class="form-control" id="search" name="search"></input>     
</div> 

    {{ Form::close() }} 

改變你的Ajax請求如下:

$('#searchform').on('submit', function(e) { 
     e.preventDefault(); 
     var search = $('#search').val(); 
     $.ajax({ 
      type: "GET", 
      url: {{URL::to('search')}}, 
      data: {search:search} 
      success:function(data){ 
         $('tbody').html(data);     
        } 
     }); 
    }); 

如果不是,那麼:

設置APP_DEBUG in .env爲true,因爲該請求是AJAX,使用Chrome,並且按F12鍵,進入網絡選項卡 - >點擊錯誤 - >預覽選項卡,如果它只是說空白屏幕錯誤,那麼也許你應該chmod 775(寫入權限),然後再試一次

+0

謝謝你的建議,現在我解決了我的問題,我必須通過localhost/laravel/public/search/search來訪問它 –