Laravel 5:LengthAwarePaginator.php中的ErrorException:除以零 - php

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

但是当我尝试将其更改为25时,我在开发人员控制台中收到此错误

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

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'=>'http://localhost/r2/public/posts/per_page' ,LengthAwarePaginator.php第48行中的'query'=> array()),'key'=>'query','value'=> array()))

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

在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(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,并且出现此错误

错误404对象未找到!

但是,如果我手动转到该URL 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('');

参考方案

每页页数(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构造函数。您需要在顺序方法per_page => per_page()()=> show_comment_list => comment_list()中检查var paginate()语句。

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

PHP Laravel从另一个驱动器读取文件 - php

我目前正在学习Laravel。我想知道是否有一种方法可以在Laravel中使用Storage::从另一个硬盘访问文件(使用Windows)。例如,我在驱动器C:上安装了带有Laravel的Xampp,但是我想访问网站目录之外的E:上的文件。我试过使用Storage::files('E:')和File::files('E:…

Laravel 5排序具有关系的雄辩模型 - php

我有一个要基于关系属性进行排序的模型。主模型Resultado具有类型respondente的关系belongsTo,而respondente具有关系usuario。我想获取具有这些关系的Resultado并按usuario name属性(列)对结果进行排序。我试过了$resultados = Diagnostico\Resultado::with(…

Laravel Nova自指关系 - php

在Laravel中,如果要创建自引用关系,可以执行以下操作:class Post extends Eloquent { public function parent() { return $this->belongsTo('Post', 'parent_id'); } public function childr…

检查对象是否已在集合中-Laravel - php

当我循环一系列不同的结果时,我希望将对象添加到新集合中。查询:$osRed = Item::where('category', 'Hardware') ->where(function ($query) { $query->where('operating_system', '…

Laravel @yield在刀片视图引擎的html标签中 - php

这可能吗?<html @yield( 'AngularNamespace' )> </html> 显然以上方法不起作用... 参考方案 这个对我有用。只要您的其他模板文件具有@section('AngularNamespace', 'NameSpaceHere')