2017-06-22 142 views
0

我在Laravel應用程序中通過AJAX請求提取分頁數據。這對第一塊很有效。但是,對於以下內容,它只傳遞一列(id)。Laravel:分頁只返回第一個塊

什麼問題?

相關的代碼:

Route::get('contacts', function (Request $request) { 
    return buildQuery($request, User::class)->simplePaginate(50); 
}); 

function buildQuery(Request $request, $model) { 
    $query = DB::table('users')->select('id as ID'); 
    $properties = $request->all(); 

    $searchString = ""; 
    if (in_array('q', array_keys($properties)) && $properties['q']) { 
     $searchString = $properties['q']; 
     $query->orWhere('id', 'like', '%'.$searchString.'%'); 
    } 

    foreach ($properties as $property => $value) { 
     if ($property == "id" || $property == "q") 
      continue; 

     if (in_array($property, array_keys($model::$labels))) { 
      $query->addSelect($property.' as '.$model::$labels[$property]); 

      if ($value == "desc" || $value == "asc") 
       $query->orderBy($property, $value); 

      if ($searchString) 
       $query->orWhere($property, 'like', '%'.$searchString.'%'); 
     } 
    } 

    return $query; 
} 

回答

1

它,因爲查詢參數不附加在隨後的分頁請求。

更新你的邏輯路徑裏面:

Route::get('contacts', function (Request $request) { 
    return buildQuery($request, User::class)->paginate(50)->appends($request->all()); 
}); 

OR

Route::get('contacts', function (Request $request) { 
     return buildQuery($request, User::class)->simplePaginate(50)->appends($request->all()); 
    }); 
+0

引出:BadMethodCallException:方法追加不存在。 –

+0

好的,我的壞。它適用於LengthAwarePaginator。如果您需要它進行簡單分頁,請使用查詢返回的結果的附加值,同時根據查詢生成鏈接。 – ayip

+0

同樣的錯誤。該對象追加被稱爲AbstractPaginator –