我有一个带有per_page
输入字段的评论列表,允许用户通过ajax在页面上显示更多评论。默认情况下,它设置为50
但是当我尝试更改它时,说到25,我在开发人员的控制台中出现了这个错误
POST http://localhost/r2/public/posts/per_page 500(内部服务器错误)
在Network
标签中,我可以看到此错误
LengthAwarePaginator.php第48行中的ErrorException:
除以零
在LengthAwarePaginator.php第48行at HandleExceptions-> handleError(' 2','除以零',' C:\ xampp \ htdocs \ r2 \ vendor \ laravel \ framework \ src \ Illuminate \ Pagination \ LengthAwarePaginator.php',' 48',数组(' items' => array(),' total' =>& #39; 0',' perPage' => null,' currentPage' =>' 1',' options' = >数组('路径' =>' http://localhost/r2/public/posts/per_page','查询' => array()),'键&在LengthAwarePaginator.php第48行中#39; =>'查询','值' => array()))
在LengthAwarePaginator-> __ construct(array(),' 0',null,' 1',array(' path' =>&#39 ; http://localhost/r2/public/posts/per_page','查询' => array()))在CommentController.php第51行
在CommentController.php第57行的CommentController-> paginate(array(),null,object(Request),object(Post))在CommentController.php第153行的CommentController-> comment_list(null,object(Request),object(Post))
在CommentController.php第164行的CommentController-> show_comment_list(object(Request),object(Post))
在CommentController-> per_page(对象(请求),对象(帖子),'帖子')
在我更改路线以将其与帖子页面集成之前,它工作正常。
我的路线
Route::get('{post}/comment', ['as' => 'comment', 'uses' => 'CommentController@index']);
Route::post('{post}/post_this_comment', 'CommentController@post_this_comment');
Route::get('{post}/recaptcha', 'CommentController@recaptcha');
Route::get('{post}/reply_comment', 'CommentController@reply_comment');
// this is the per_page route
Route::post('{post}/per_page', ['as' => 'per_page', 'uses' => 'CommentController@per_page']);
Route::post('{post}/comment/update', ['as' => 'comment/update', 'uses' => 'CommentController@update']);
这是CommentController
private function paginate($items, $perPage, Request $request) {
$page = Input::get('page', 1); // get current page or default to 1
$offset = ($page * $perPage) - $perPage;
return new LengthAwarePaginator(
array_slice($items, $offset, $perPage, false),
count($items),
$perPage,
$page,
['path' => $request->url(), 'query' => $request->query()]);
}
protected function comment_list($per_page, Request $request, Post $post) {
$root_comments = Comment::root_comments($post->id);
$root_with_replies = $this->include_replies_for($root_comments);
$paginated_comments = $this->paginate($root_with_replies, $per_page, $request, $post);
return $paginated_comments;
}
protected function show_comment_list(Request $request, Post $post) {
$per_page = Input::get('per_page');
session(['per_page' => $per_page]);
$comment_list = view('eastgate.comment.comment_list')
->with('comments', $this->comment_list($per_page, $request, $post))
->with('total_comments', $this->total_comments())
->with('per_page', $per_page)
->render();
return $comment_list;
}
public function per_page(Request $request, Post $post){
$response = array(
'status' => 'success',
'msg' => 'reply comment',
'comment_list' => $this->show_comment_list($request, $post)
);
return Response::json($response);
}
这是JS和HTML
$(document).on('change', 'input.comments_per_page', function(){
var formData = new FormData();
formData.append('per_page', $('.comments_per_page').val());
var request = $.ajax({ // push question data to server
type : 'POST', // define the type of HTTP verb we want to use (POST for our form)
url : 'per_page', // the url where we want to POST
data : formData,
dataType : 'json',
processData : false,
contentType : false
});
request.done(per_page_done_handler);
request.fail(per_page_fail_handler); // fail promise callback
});
<div class="col-xs-12">
Show <input type="text" name="comments_per_page" class="comments_per_page" value="{!! $per_page !!}" size="2" title="Number of comments per page"> comments per page
</div>
更新
我应该提一下,我也可以看到laravel的默认分页div,当我点击第二页,其中http://localhost/r2/public/posts/2/?page=2
的网址重定向到http://localhost/posts/2?page=2
,我收到此错误
ERROR 404找不到对象!
但是,如果我手动转到此网址http://localhost/r2/public/posts/2?page=2
带有评论的第二页加载得很好。
更新2
我只是setPath
在$paginated_comments
方法中comment_list()
,现在接下来的页面正常。但是当我尝试更改显示的评论数时仍然出现Division by zero
错误。
$paginated_comments = $this->paginate($root_with_replies, $per_page, $request, $post);
$paginated_comments->setPath('');
答案 0 :(得分:3)
per_page编号(50或25)无关紧要。如果您查看\vendor\laravel\framework\src\Illuminate\Pagination\LengthAwarePaginator.php
,则可以看到问题行(它会引发异常Division by zero
):
$this->lastPage = (int) ceil($total / $perPage); // <= problem
这只意味着一件事 - $perPage
为0或null =&gt;您没有将per_page
(25)的预期值传递给LengthAwarePaginator构造函数。您需要检查var per_page
语句,方法per_page()
=&gt; show_comment_list
()=&gt; comment_list()
=&gt; paginate()
。
P.S。 imho,它应该是一个单一的分页行动。您需要完成所有必需的信息,但是,您将其分为四个“部分”。
答案 1 :(得分:1)
我认为你的问题是你没有告诉你的路线per_page是一个参数,让我解释一下:
Route::post('{post}/{per_page}', ['as' => 'per_page', 'uses' => 'CommentController@per_page']);
应该是: {per_page}
{{1}}
我希望这可以帮到你。