0

Please look into my code, this is my 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'));
});

Here is my blade file,

<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>

My problem is that, when I refresh my page, the URL is http://localhost/test?page=1

And when I click on any link, the URL is just replacing with its number (http://localhost/test?page=2), not redirecting.

I inspect the pagination link, it seems like

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

It is working when I added and attribute to <a> tag, that target="_self".

But how can I add this attribute in Laravel pagination URL or is there any other way to solve this issue?

David Buck
  • 3,752
  • 35
  • 31
  • 35
Sandeep Sudhakaran
  • 1,072
  • 2
  • 9
  • 22

1 Answers1

0

In Laravel 7.x you can customize URL in route file or controller like:

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

OR

Also append sort=votes to each pagination link, you should make the following call to appends in view, like:

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

Check this out the laravel.com/docs/7.x/pagination#displaying-pagination-results documentation link

Suhrab Y
  • 143
  • 2
  • 8