Laravel搜索路线

时间:2017-08-14 19:51:44

标签: laravel routes laravel-controller

我在关于实时搜索的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('/' ,'SearchController@index');
Route::get('/search' ,'SearchController@search');

在我的资源文件夹中 我有文件夹搜索,它包含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('/' ,'SearchController@index');

但如果尝试将此更改为

 Route::get('search' ,'SearchController@index');

我收到错误500

对此进行路由的正确方法是什么,因此不会使用索引

谢谢

1 个答案:

答案 0 :(得分: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,转到网络标签 - &gt;点击错误 - &gt;预览标签,如果它只是用空白屏幕说错误,那么也许你应该chmod 775(写入权限)并再试一次