【问题标题】:Laravel pagination page 2 loses the dataLaravel 分页第 2 页丢失数据
【发布时间】:2018-03-28 14:14:35
【问题描述】:

当我填写我的搜索并发布它时,它会获取数据并返回它。 在这个查询中,我使用 ->paginate(50);

$products = Product7Days::where('omschrijving', 'LIKE', '%'.$request->omschrijving.'%')
                                    ->where('artikelnummer', 'LIKE', '%'.$request->artikelnummer.'%')
                                    ->whereIn('relatienummer', $leverancierenArray)
                                    ->whereIn('omzetgroep', $omzetgroepenArray)
                                    ->whereIn('btwcode', $BTWArray)
                                    ->whereBetween('prijs', [$prijsVan, $prijsTot])
                                    ->where('totaalaantal', '>=', $aantalVan)
                                    ->where('totaalaantal', '<=', $aantalTot)
                                    ->paginate(50);

我的路线

Route::get('/artikelen', 'ProductController@index')->name('artikelen');

Route::post('/artikelen', 'ProductController@search')->name('artikelen.search');

The pagination on the page.

所以一切正常,但是当我转到第二页时,由于某种原因没有结果

当我搜索时,它会转到此页面 http://172.16.0.51:8000/artikelen

当我点击第 2 页时,链接是 http://172.16.0.51:8000/artikelen?page=2 但没有数据

刀片文件

@if (isset($products))
                    <table class="table table-striped table-responsive-xl">
                        <thead class="thead-dark">
                            <tr>
                                <th>Artikelnummer</th>
                                <th>Omschrijving</th>
                                <th>Prijs</th>
                                <th>Totaal <br> verkocht</th>
                                <th>Totaal <br> omzet</th>
                                @foreach ($filialen as $filiaal)
                                @if ($filiaal !== 6)
                                <th>aantal. <br>F{{$filiaal}}</th>
                                <th>voorr. <br>F{{$filiaal}}</th>
                                @else
                                <th>voorr. <br>F{{$filiaal}}</th>
                                @endif
                                @endforeach
                            </tr>
                        </thead>
                        <tbody>
                        @foreach ($products as $product)
                            <tr>
                                <th>{{ $product->artikelnummer}}</th>
                                <th>{{ $product->omschrijving}}</th>
                                <th>{{ $product->prijs}}</th>
                                <th>{{ $product->totaalaantal}}</th>
                                <th class="text-right">{{ round($product->totaalaantal * $product->prijs, 1) }}</th>
                                @foreach ($filialen as $filiaal)
                                @if ($filiaal !== 6)
                                <th class="text-right">{{ $product["aantalF".$filiaal] }}</th>
                                <th class="text-right">{{ $product["voorraadF".$filiaal] }}</th>
                                @else
                                <th>{{ $product->voorraadF6 }}</th>
                                @endif
                                @endforeach
                            </tr>
                        @endforeach
                        </tbody>
                    </table>
                    {{ $products->links() }}
                    @endif

isset() 返回 false...

我很确定当我点击第 2 页时,数据会丢失,但我的问题是如何提供这些数据

【问题讨论】:

  • @WahyuKristianto 没什么可看的,最重要的是你有我要问的所有问题,下面应该是第 2 页的所有数据
  • 不使用GET参数是否可以访问articleen路由?类似?omschrijving=xxx&amp;artikelnummer=xxx

标签: php laravel pagination request


【解决方案1】:

你需要防止 Laravel 中的分页链接的默认行为,这个独奏适用于像 http://172.16.0.51:8000/search?property1=x&amp;property2=y 这样的搜索 url

$(document).on('click', '.pagination li a', function (e) {
    e.preventDefault();
    if ($(this).attr('href')) {
        var queryString = '';
        var allQueries = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');
        if(allQueries[0].split('=').length >1){
            for (var i = 0; i < allQueries.length; i++) {
                var hash = allQueries[i].split('=');
                if (hash[0] !== 'page') {
                    queryString += '&' + hash[0] + '=' + hash[1];
                }
            }
        }
        window.location.replace($(this).attr('href') + queryString);
    }
});

【讨论】:

  • 但这对我没有帮助吗?因为我的链接是 /artikelen?page=2
  • 你为什么不试试呢?
  • 我试过了,链接保持不变,没有给出结果。也许我做错了?\
  • 在搜索结果中使用paginate() 而不是get(),这样它们都会创建分页链接,当点击链接时,它会转到正确的路线,如果有查询字符串则转到搜索功能,否则转到其他功能
  • 等等,我很困惑,我没有使用 get()?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-09-08
  • 2016-11-28
相关资源
最近更新 更多