简体   繁体   English

Laravel PHP:无法正确检索结果的其他分页页面

[英]Laravel PHP: Additional Pagination Pages of results not being retrieved properly

I'm working on a Laravel PHP application where on the home page, the user selects a particular category and results are returned after the user makes their selection. 我正在使用Laravel PHP应用程序,在该应用程序的主页上,用户选择了特定的类别,并在用户做出选择后返回结果。 Right now results are being returned properly except for one issue where if there are multiple pages of results. 目前,结果将正确返回,但如果有多个结果页的问题除外。

For example, let's say I have 15 records that belong to 'category1'. 例如,假设我有15条属于“ category1”的记录。 Right now I have the pagination set up so that 10 results are displayed per page. 现在,我已经设置了分页,以便每页显示10个结果。 So these 15 records should be displayed as follows, Page 1 contains 10 records and Page 2 contains 5 records. 因此,这15条记录应显示如下,第1页包含10条记录,第2页包含5条记录。 However the problem I'm having is that Page 1 displays the first 10 records for the selected category fine but when selecting Page 2, it displays ALL of the additional records and not just the remaining 5 records that belong to 'category1'. 但是,我遇到的问题是,第1页很好地显示了所选类别的前10条记录,但是当选择第2页时,它显示了所有其他记录,而不仅仅是属于“ category1”的其余5条记录。

Controller: 控制器:

 <!-- Form -->

 {{Form::model(array('method' => 'GET', 'route' => 'overview_select', 'files' => true)) }}    

 <div class="form-group">
 {{ Form::label('category', 'Category:') }}
 {{ Form::select('category', array('category1' => 'Category1', 'category2' => 'Category2', 'category3' => 'Category3', 'category4' => 'Category4'), (isset($category) ? $category : 'category')) }}

 <div class="form-group">
 {{ Form::submit('Select Category', array('class' => 'btn btn-primary')) }}
 </div>

 {{ Form::close() }}

 </div>

   public function index()
{
    $vdo = Video::query();
    $pic = Picture::query();
    if($category = Input::get('category')){
        $vdo->where('category', $category);
        $pic->where('category', $category);
    }

    $allvids = $vdo->paginate(10);
    $allpics = $pic->paginate(10);
    $data = compact('allvids','allpics');
    $data['category'] = Input::get('category');
    $this->layout->content = \View::make('home.pics_vids_overview',$data);
}

View: 视图:

<div class="panel-body">
            @if ($allvids->count())
                <dl class="dl-horizontal">
                    <!-- Add this div class to make the table responsive -->
                    <div class = "table-responsive">
                        <table class="table">
                            <b>Results: </b>{{ $allvids->count() }}<br>
                            <tr>
                                <th>ID</th>
                                <th>Name</th>
                                <th>Description</th>
                                <th>Corner</th>
                                <th>Rock</th>
                                <th>Category</th>
                                <th>Project</th>
                                <th>Update Video</th>
                            </tr>        
                    @foreach($allvids as $video)
                    <tr>
                         <td>{{ $video->video_id }}</b></td>
                         <td>{{ $video->video_name }}</td>
                         <td>{{ $video->video_description }}</td>
                         <td>{{ $video->video_corners }} </td>
                         <td>{{ $video->video_rocks }}</td>
                         <td>{{ $video->category }}</td>
                         <td>{{ $video->video_project }}</td>

                         <td><b>{{ link_to_route("show_video", 'Edit', array($video->video_id)) }}</b></td>
                    </tr>     
                    @endforeach
                    {{ $allvids-> links()}}
                        </table>
                    </div>    


                </dl>
            @else
                <b>{{ 'No Videos in this category.' }}</b>
            @endif
        </div>

Routes.php routes.php文件

Route::post('pics_vids', array('as'=>'overview_select', 'uses' => 'OverviewController@index'));

Route::get('pics_vids', array('as'=>'overview', 'uses' => 'OverviewController@index'));

I'm not quite sure if I need to append anything within my controller in order for additional pages to only display results from the selected category. 我不太确定是否需要在控制器内添加任何内容,以使其他页面仅显示所选类别的结果。 Any help is greatly appreciated! 任何帮助是极大的赞赏!

I believe the problem is in your process of retrieving the pagination based on category . 我相信问题出在您根据类别检索分页的过程中。 It is better to use GET method to retrieve listing results, because using POST method for pagination listing might throw form resubmission warning in some browsers when users hit back button. 最好使用GET方法检索列表结果,因为使用POST方法进行分页列表可能会在用户单击“后退”按钮时在某些浏览器中引发表单重新提交警告。

Now, the reason you are getting all the products in page 2 is 现在,获得第2页中所有产品的原因是

   if($category = Input::get('category')){
    $vdo->where('category', $category);
    $pic->where('category', $category);
}

Here, when the second link is clicked, the request is not POSTed anymore, so the Input::get() method is empty. 在这里,当单击第二个链接时,该请求不再发布,因此Input :: get()方法为空。 Change the routing so that the category forms as a part of URL, either in the form of query string or URI string. 更改路由,以使类别以查询字符串或URI字符串的形式作为URL的一部分形成。

EDIT: 编辑:

I changed the form in the view from: 我从以下位置更改了视图中的表单:

{{Form::model(array('method' => 'GET', 'route' => 'overview_select', 'files' => true)) }}    

to: 至:

{{Form::open(array('method' => 'GET', 'route' => 'overview_select', 'files' => true)) }}

So now the 'category' field will appear in the URL after the user makes their selection. 因此,现在,用户进行选择后,“类别”字段将出现在URL中。

In the view, I changed 在视图中,我改变了

{{ $allvids-> links()}}

to: 至:

{{ $allvids->appends(array('category' => $category))->links()}}

And now the pagination will work for additional pages. 现在,分页将适用于其他页面。

The link below helped me out with my solution and also gives a good explanation on how GET/POST methods work in forms for Laravel 4. 下面的链接为我提供了解决方案,并很好地解释了GET / POST方法如何在Laravel 4的表单中工作。

Pagination in Laravel 4 works for one page, but not working for another Laravel 4中的分页适用于一页,但不适用于另一页

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

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