Laravel pagination {{$users->links}} 不存在



我设置了一个视图编辑器,当我在使用时尝试分页时,它会将用户数据提供给侧边栏

User::paginate(1)

并将$users->链接放入刀片中,它说找不到链接,但是当我使用

User::all()

它可以工作并且没有错误,但我无法对表进行分页,我看不到错误来自何处。 这是错误显示:

调用未定义的方法 App\User::links() (查看: C:\Users\kaasv\medialab\resources\views\layouts\sidebar.blade.php) (查看: C:\Users\kaasv\medialab\resources\views\layouts\sidebar.blade.php) (查看: C:\Users\kaasv\medialab\resources\views\layouts\sidebar.blade.php)

应用服务提供商

   use IlluminateSupportServiceProvider;
   use IlluminateSupportFacadesSchema;
   //use IlluminateViewView;
   use View;
   //use AppRepositoriesUserRepository;
   use AppUser;
  class AppServiceProvider extends ServiceProvider
{
/**
 * Bootstrap any application services.
 *
 * @return void
 */
public function boot()
{
    Schema::defaultStringLength(191);
    View::share('user', User::paginate(1));
}
/**
 * Register any application services.
 *
 * @return void
 */
public function register()
{
    //
}
}

侧边栏刀片.php

    <h1>Alle Studenten</h1>
  @if (Auth::check())

<table class="table">
    <tr>
        <th scope="col">#</th>
        <th scope="col">Name</th>
        <th scope="col">Student Number</th>
    </tr>
    @foreach($user as $users)
        <tr>
            <th scope="row">
               {{$users->id}}
            </th>
            <th>
                <a class="text-dark" href="/admin/leerling/{{ $users->id }}">
                    {{$users->name}}
                </a>
            </th>
            <th>
                {{$users->student_number}}
            </th>
        </tr>
    @endforeach
</table>
{{ $users->links() }}
     @endif
首先,

我不建议使用 Laravel's View Composers,因为它们将在每个视图上初始化,即使是您添加的视图 @include@extends 等。如果您从 Composers 共享的数据是从数据库中提取的,则可能会导致性能受到很大影响。

其次,paginate()方法接受number of results to return作为参数,这意味着通过仅返回1用户将解析为Paginator实例,该实例实际上具有您正在寻找的方法:links(),但问题是您在视图中访问users user命名

最新更新