在一个页面中显示 laravel 分页链接两次

Display laravel Pagination links twice in a page

我在我的一个项目中实施了 laravel 分页,其中分页功能非常棒。

我需要在 table 的顶部和 table 的底部显示分页链接。 像这样

{!! $entries->render() !!}
<table class="table table-responsive" id="entries-table">
    <thead>
        <tr>
                <th>ID</th>
                <th>Advisor name</th>
        </tr>
    </thead>
    <tbody>
    @forelse($entries as $entries)
        <tr>
            <td> {{ $entries->id }} </td>
            <td> {{ $entries->name }} </td>
        </tr>
    @empty
    <tr>
        <td>
            <p>No record found.</p>
        </td>
    </tr>
    @endforelse
    </tbody>
</table>
{!! $entries->render() !!}

当我尝试在 table 的底部或同一页面的任何地方再次使用 {!! $entries->render() !!} 时,它会抛出以下错误。

ErrorException (E_ERROR) Call to undefined method App\Models\Entries::render()

这是我的控制器代码

public function index(Request $request)
    {            
        $entries = Entries::orderBy('id', 'DESC')->paginate(15);

        return view('entries.index')
            ->with('entries', $entries);
    }

我在这里使用 dd($entries) 将变量 $entries 转储到控制器中,这就是我得到的结果。

LengthAwarePaginator {#425 ▼
  #total: 215
  #lastPage: 15
  #items: Collection {#436 ▼
    #items: array:15 [▼
      0 => entries {#437 ▶}
      1 => entries {#438 ▶}
      2 => entries {#439 ▶}
      3 => entries {#440 ▶}
      4 => entries {#441 ▶}
      5 => entries {#442 ▶}
      6 => entries {#443 ▶}
      7 => entries {#444 ▶}
      8 => entries {#445 ▶}
      9 => entries {#446 ▶}
      10 => entries {#447 ▶}
      11 => entries {#448 ▶}
      12 => entries {#449 ▶}
      13 => entries {#450 ▶}
      14 => entries {#451 ▶}
    ]
  }
  #perPage: 15
  #currentPage: 1
  #path: "https://samplesite.com/entries/11"
  #query: []
  #fragment: null
  #pageName: "page"
  +onEachSide: 3
  #options: array:2 [▼
    "path" => "https://samplesite.com/entries/11"
    "pageName" => "page"
  ]
}

检查此 Video 我正在与您分享更好的想法

你试过了吗{{ $entries->links() }}

更新

在你的控制器中你能试试这个吗

    $users = User::paginate(1);
    dd($users->links(), $users->links());

你有同样的错误吗?

您不应该在 foreach 的变量中赋予相同的名称,这可能会导致冲突 between.errors,因为 eloquent 结果或结果中的变量的 abigu 可能会发生在循环中。

尝试像这样重构您的代码

@forelse($entries as $entrie)
    <tr>
        <td> {{ $entrie->id }} </td>
        <td> {{ $entrie->name }} </td>
    </tr>
@empty
<tr>
    <td>
        <p>No record found.</p>
    </td>
</tr>
@endforelse

确保您在 blade 中返回准确的 collection。

$users= User::paginate(15);

return view('entries', compact('users'));

并且您已经可以在 blade 中使用这一行。

{!! $entries->links() !!}

{!! $users->render() !!}

并且在你的每一个中,将其更改为这个

@forelse($entries as $entry)
     <tr>
         <td> {{ $entry->id }} </td>
         <td> {{ $entry->name }} </td>
     </tr>
@empty

尝试将您的控制器更改为此。

  public function index(Request $request)
    {            
            $entries = entries::where('form_id', '=', $id)->orderBy('id', 'DESC')->paginate(15);

            return view('entries.index')
                ->with('entries', $entries);
    }

您正在用 @forelse 中的 collection 项覆盖 $entries collection。

@forelse($entries as $entries)
    <tr>
        <td> {{ $entries->id }} </td>
        <td> {{ $entries->name }} </td>
    </tr>
@empty

$entries更改为$entry:

@forelse($entries as $entry)
    <tr>
        <td> {{ $entry->id }} </td>
        <td> {{ $entry->name }} </td>
    </tr>
@empty