【问题标题】:Add attributes to Laravel pagination links为 Laravel 分页链接添加属性
【发布时间】:2019-04-16 14:16:25
【问题描述】:

请查看我的代码, 这是我的 web.php

Route::get('test', function (Request $request) {
  $data = MyTableResource::collection(MyTable::paginate(10));
  $headers = ['col1', 'col2', 'col3'];
  return view('test.index', compact('data', 'headers'));
});

这是我的刀片文件,

<table md-table>
    <thead>
        <tr>
            @foreach($headers as $header)
                <th><span>{{$header}}</span></th>
            @endforeach
        </tr>
    </thead>
    <tbody>
        @foreach($data as $d)
            <tr>
                <td>{{$d->id}}</td>
                <td>{{$d->primary_identifier}}</td>
                <td>{{$d->order_date}}</td>
            </tr>
        @endforeach
    </tbody>
</table>
<div class = "table-paginate">
    {{ $orders->links() }}
</div>

我的问题是, 当我刷新我的页面时,网址是 http://localhost/test?page=1

当我点击任何链接时,URL 只是替换为它的编号 (http://localhost/test?page=2),而不是重定向。

我检查了分页链接,好像

<li class="page-item" aria-current="page">
  <a class="page-link" href="http://amzmarketplace.localhost.tom/test?page=2">2</a>
</li>

当我添加并归因于&lt;a&gt; 标签时,它正在工作,target="_self"

但是如何在 Laravel 分页 URL 中添加这个属性,或者有没有其他方法可以解决这个问题?

【问题讨论】:

标签: php laravel laravel-pagination laravel-paginate


【解决方案1】:

在 Laravel 7.x 中,您可以在路由文件或控制器中自定义 URL,例如:

     Route::get('test', function (Request $request) {
      $data = MyTableResource::collection(MyTable::paginate(10));
      $data->withPath('custom/url');
...
    });

还要将sort=votes 附加到每个分页链接,您应该在视图中对appends 进行以下调用,例如:

{{ $data->appends(['sort' => 'votes'])->links() }}

查看laravel.com/docs/7.x/pagination#displaying-pagination-results 文档链接

【讨论】:

    猜你喜欢
    • 2019-08-26
    • 2011-06-01
    • 1970-01-01
    • 2016-12-26
    • 2020-10-12
    • 2020-03-22
    • 2011-11-01
    • 2017-01-29
    • 2012-11-01
    相关资源
    最近更新 更多