Laravel 5: Как разбивать Ajax-ответ

Я делаю поиск, и на основании этого я возвращаю некоторые данные через Jquery-Ajax. Нет никаких проблем, чтобы отображать данные, но мне они нужны как разбитые на страницы.

JQuery

$(document).ready( function() { $(".client_search_option").change(function(){ var selectedClientTypeVal = ""; var selectedSmsDecisionVal = ""; var selectedClientType = $('input[type=radio][name=clientType]:checked'); var selectedSmsDecision = $('input[type=radio][name=sms_decision]:checked'); if (selectedClientType.length > 0) { selectedClientTypeVal = selectedClientType.val(); } if (selectedSmsDecision.length > 0) { selectedSmsDecisionVal = selectedSmsDecision.val(); } //alert(selectedClientTypeVal); //alert(selectedSmsDecisionVal); var CSRF_TOKEN = $('meta[name="csrf-token"]').attr('content'); $.ajax({ url: 'http://localhost/pages/clientSearchAjax', type: 'POST', data: {_token: CSRF_TOKEN, selectedClientTypeVal:selectedClientTypeVal,selectedSmsDecisionVal:selectedSmsDecisionVal}, dataType: 'JSON', success: function (data) { console.log(data); }, error:function(){ alert("An error has occured !"); } }); }); }); 

контроллер

 public function clientSearch(){ $client_option = Input::get('selectedClientTypeVal'); $sms_option = Input::get('selectedSmsDecisionVal'); if($client_option == 'all' && $sms_option == 'all'){ $ajax_clients = Client::with('clientType')->paginate(5); }else{ $ajax_clients = Client::with('clientType')->where('clienttype_id', $client_option)->where('send_sms', $sms_option)->paginate(5); } return $ajax_clients->toJson(); } 

Как я могу сделать paginate Ajax Response, любая помощь будет оценена.

Я был в той же ситуации, и после небольшого исследования я вышел со следующей ссылкой. Может быть, это может помочь вам.

Метод контроллера

 public function showPosts() { $posts = Post::paginate(5); if (Request::ajax()) { return Response::json(View::make('posts', array('posts' => $posts))->render()); } return View::make('blog', array('posts' => $posts)); } 

Часть jQuery

 <script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js"></script> <script> $(window).on('hashchange', function() { if (window.location.hash) { var page = window.location.hash.replace('#', ''); if (page == Number.NaN || page <= 0) { return false; } else { getPosts(page); } } }); $(document).ready(function() { $(document).on('click', '.pagination a', function(e) { getPosts($(this).attr('href').split('page=')[1]); e.preventDefault(); }); }); function getPosts(page) { $.ajax({ url: '?page=' + page, dataType: 'json', }).done(function(data) { $('.posts').html(data); location.hash = page; }).fail(function() { alert('Posts could not be loaded.'); }); } </script> 

Проверить эту ссылку