在jQuery和Laravel4中使用getJSON进行动态搜索

在jQuery和Laravel4中使用getJSON进行动态搜索,laravel,laravel-4,Laravel,Laravel 4,我正在尝试进行动态搜索。当用户在输入字段中输入字符时,结果将从模型中恢复。我不会使用以下代码返回结果: //here is my HTLML: <input type='text' placeholder='Search By Email' class='searchterm' > //here is my jQuery: $(".searchterm").keyup(function(e){ var q = $(".searchterm").val(); $.getJS

我正在尝试进行动态搜索。当用户在输入字段中输入字符时,结果将从模型中恢复。我不会使用以下代码返回结果:

//here is my HTLML:
<input type='text' placeholder='Search By Email' class='searchterm' > 

//here is my jQuery:
$(".searchterm").keyup(function(e){
  var q = $(".searchterm").val();
  $.getJSON("searchpeeps",
  {
    q: q,
  },
  function(data) {
    $("ul.peepList").empty();
    $("ul.peepList").append(" Results for <b>" + q + "</b>");
    $.each(data.query.search, function(i,item){
      $("ul.peepList").append("<li>"++"</li>");
    });
  });
 });


//here is my get route:
Route::get('searchpeeps/{q}', array('as' => 'searchpeeps', 'uses' => 'PeopleController@searchpeeps'));


//here is my controller:
    public function searchpeeps($id)
    {

        $uid = Auth::user()->id;
        //$q = Input::get('q');
        return Invite::where('email', $id)
        ->where('user_id', $uid)    
        ->orderBy("created_at", "desc")
        ->groupBy('email')
        ->get(); 
    }
//这是我的HTLML:
//以下是我的jQuery:
$(“.searchterm”).keyup(函数(e){
var q=$(“.searchterm”).val();
$.getJSON(“搜索peeps”,
{
问:问,,
},
功能(数据){
$(“ul.peepList”).empty();
$(“ul.peepList”).append(“用于“+q+”的结果”);
$.each(data.query.search,函数(i,项){
$(“ul.peepList”)。追加(“
  • ”++“
  • ”); }); }); }); //以下是我的获取路线: 路由::get('searchpeeps/{q}',array('as'=>'searchpeeps','uses'=>'PeopleController@searchpeeps')); //这是我的控制器: 公共函数searchpeeps($id) { $uid=Auth::user()->id; //$q=输入::获取('q'); return Invite::where('email',$id) ->其中('user_id',$uid) ->订购人(“创建于”,“描述”) ->groupBy(“电子邮件”) ->get(); }
    您没有注册正确的URL。 解决此问题的两种方法&编写一些代码以更好地理解(根据需要调整)

    1。调整PHP

    // Here, modified URL
    Route::get('searchpeeps', function(){
        return Input::get('q');
    });
    
    使用此Javascript

    $(".searchterm").keyup(function(e){
        $.getJSON(
            "searchpeeps",
            { q: this.value },
            function(data) {
                console.log(data);
            }
        );
    });
    
    2。调整Javascript

    $(".searchterm").keyup(function(e){
        $.getJSON(
            "searchpeeps/" + this.value,
            null,
            function(data) {
                console.log(data);
            }
        );
    });
    
    。。。用这个PHP

    Route::get('searchpeeps/{q}', function($q){
        return $q;
    });