向laravel分页链接添加属性

时间:2019-04-16 14:16:26

标签: php laravel laravel-pagination laravel-paginate

请查看我的代码, 这是我的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>

我的问题是, 当我刷新页面时,URL为  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>

当我向<a>标签添加target="_self"并将其归于属性时,它就起作用了。

但是如何在laravel分页网址中添加此属性,或者有其他方法可以解决此问题?

预先感谢

1 个答案:

答案 0 :(得分:0)

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

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

OR

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

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

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

相关问题