简体   繁体   English

Laravel 5 Paginator仅返回首页

[英]Laravel 5 Paginator returns first page only

I have a fairly simple chatlog viewer which uses a Laravel paginator to easily separate the returned logs into pages. 我有一个相当简单的聊天日志查看器,它使用Laravel分页器轻松将返回的日志分成页面。 Unfortunately, the paginator only ever returns the first page of results, even though it renders the number of pages correctly. 不幸的是,即使分页器正确显示了页面数,也只会返回结果的第一页。

Here's the controller code: 这是控制器代码:

public function search(Requests\ChatSearchRequest $request)
{
    //split search up into what we've got

    $request->flash();

    $nick = $request->input('nick');
    $message = $request->input('message');
    $channel = $request->input('channel');
    $time_start = $request->input('datestart');
    $time_end = $request->input('dateend');
    $sortby = $request->input('sortby');
    $limit = (int) $request->input('limit');
    $page = (int) $request->input('page');


    $query = ChatMessage::query();

    if (!empty($nick))
        $query->usersLike($nick);
    if (!empty($channel))
        $query->fromChannel($channel);
    if (!empty($message))
        $query->matchMessage($message);
    if (!empty($time_start))
        $query->betweenTimes($time_start, $time_end);

    $query->orderBy('id', $sortby);

    $messageResults = $query->take($limit)->get();

    $messages = new LengthAwarePaginator($messageResults->all(), $messageResults->count(), 100, $page, ['path' => Paginator::resolveCurrentPath(), 'query' => $request->query()]);

    return view('chatlog', compact('messages'));
}

And here's what a request generated by the form the page uses looks like: 这是页面使用表单生成的请求的样子:

http://localhost/chatlog/search?message=&nick=Saten&channel=&datestart=2017-09-17T14%3A50%3A44&dateend=2017-09-24T14%3A50%3A44&limit=250&sortby=desc&page=3

I've tried a bunch of things from other questions asked here to no avail. 我尝试了其他问题,但都没有成功,尝试了很多方法。 I've tried using Paginator::resolveCurrentPage() in lieu of pulling page from requests directly, no success. 我尝试使用Paginator::resolveCurrentPage()代替直接从请求中提取页面,但没有成功。 Nothing seems to make it work. 似乎没有任何效果。

Oddly enough, the index view on the same controller which uses a simplePaginate works fine. 奇怪的是,使用simplePaginate的同一控制器上的索引视图可以正常工作。 The form is built with Laravel Collective forms, could it be omitting the csrf_token and making the request bounce at the request side? 该表单是使用Laravel Collective表单构建的,是否可以省略csrf_token并使请求在请求侧退回?

You always take first 250 records for all pages with this query: 您始终使用此查询为所有页面获取前250条记录:

$query->take($limit)->get()

So try just add offset like this: 因此,尝试像这样添加偏移量:

$query->skip($limit*$page)->take($limit)->get()

It should skip records for previous pages and you will get only records for current page. 它应该跳过前一页的记录,并且您只会获得当前页面的记录。

Also you pass wrong data to LengthAwarePaginator constructor. 另外,您将错误的数据传递给LengthAwarePaginator构造函数。 Try this: 尝试这个:

    $messages = new LengthAwarePaginator(
        $messageResults->all(),
        ChatMessage::count(),//total count
        $limit,//items per page 
        $page,//current page
        ['path' => Paginator::resolveCurrentPath(), 'query' => $request->query()]
    );

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM