【问题标题】:Laravel 5: ErrorException in LengthAwarePaginator.php: Division by zeroLaravel 5:LengthAwarePaginator.php 中的 ErrorException:除以零
【发布时间】:2016-01-24 07:32:35
【问题描述】:

我有一个带有per_page 输入字段的 cmets 列表,允许用户通过 ajax 在页面上显示更多 cmets。默认设置为50

但是当我尝试更改它时,比如 25,我在开发者的控制台中收到了这个错误

POST http://localhost/r2/public/posts/per_page500(内部服务器错误)

Network 标签中,我可以看到这个错误

LengthAwarePaginator.php 第 48 行中的 ErrorException:

除以零

在 LengthAwarePaginator.php 第 48 行

在 HandleExceptions->handleError('2', '除以零', 'C:\xampp\htdocs\r2\vendor\laravel\framework\src\Illuminate\Pagination\LengthAwarePaginator.php', '48', array('items' => array(), 'total' => '0', 'perPage' => null, 'currentPage' => '1', 'options' => array('path' => '@ 987654322@', 'query' => array()), 'key' => 'query', 'value' => array())) 在 LengthAwarePaginator.php 第 48 行

在 LengthAwarePaginator->__construct(array(), '0', null, '1', array('path' => 'http://localhost/r2/public/posts/per_page', 'query' => array())) 在 CommentController.php第 51 行

在 CommentController.php 第 57 行中的 CommentController->paginate(array(), null, object(Request), object(Post))

在 CommentController->comment_list(null, object(Request), object(Post)) 在 CommentController.php 第 153 行

在 CommentController->show_comment_list(object(Request), object(Post)) 在 CommentController.php 第 164 行

在 CommentController->per_page(object(Request), object(Post), 'posts')

在我更改其路线以将其与帖子页面集成之前,它运行良好。

我的路线

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,当我点击 URL 为 http://localhost/r2/public/posts/2/?page=2 的第二页时,页面重定向到 http://localhost/posts/2?page=2 并出现此错误

ERROR 404 找不到对象!

但是如果我手动转到这个网址http://localhost/r2/public/posts/2?page=2

带有 cmets 的第二页加载正常。

更新 2

我只是在$paginated_comments 上使用comment_list() 方法setPath,现在下一页可以正常打开。但是当我尝试更改显示的 cmets 数量时仍然收到 Division by zero 错误。

$paginated_comments = $this->paginate($root_with_replies, $per_page, $request, $post);
$paginated_comments->setPath('');

【问题讨论】:

    标签: php jquery laravel laravel-5 laravel-5.1


    【解决方案1】:

    每页数(50 或 25)无关紧要。如果您查看\vendor\laravel\framework\src\Illuminate\Pagination\LengthAwarePaginator.php,您可以看到您的问题行(它会抛出异常Division by zero):

    $this->lastPage = (int) ceil($total / $perPage); // <= problem
    

    这仅意味着一件事 - $perPage 为 0 或 null => 您没有将 per_page (25) 的预期值传递给 LengthAwarePaginator 构造函数。您需要在 order 方法中检查 var per_page 语句 per_page() => show_comment_list() => comment_list() => paginate()

    附:恕我直言,它应该是分页的一个单一动作。您拥有制作它所需的所有信息,但是,您将它分为四个“部分”。

    【讨论】:

      【解决方案2】:

      我认为你的问题是你没有告诉你的路线 per_page 是一个参数,让我解释一下:

      Route::post('{post}/per_page', ['as' => 'per_page', 'uses' => 'CommentController@per_page']);
      

      应该是:{per_page}

      Route::post('{post}/{per_page}', ['as' => 'per_page', 'uses' => 'CommentController@per_page']);
      

      希望对你有帮助。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2019-10-17
        • 1970-01-01
        • 1970-01-01
        • 2015-07-05
        • 2016-04-09
        • 2015-10-09
        • 2017-10-29
        相关资源
        最近更新 更多