【问题标题】:Laravel pagination {{$users->links}} doesnt existLaravel 分页 {{$users->links}} 不存在
【发布时间】:2019-01-24 21:31:21
【问题描述】:

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

User::paginate(1)

并将 $users->links 放入刀片中,提示未找到链接,但当我使用时

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)

AppServiceProvider

   use Illuminate\Support\ServiceProvider;
   use Illuminate\Support\Facades\Schema;
   //use Illuminate\View\View;
   use View;
   //use App\Repositories\UserRepository;

   use App\User;

  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()
{
    //
}
}

sidebar.blade.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

【问题讨论】:

  • 没有用户,但用户:{{ $user-&gt;links() }}
  • 您的变量命名是造成混淆的原因之一。为什么将分页集合命名为 user 而将单个项目命名为 users
  • @Devon 是的,应该可以改一下,谢谢!

标签: php laravel laravel-5.7


【解决方案1】:

首先,我不建议使用Laravel's View Composers,因为它们会在每个视图上进行初始化,即使是您使用@include@extends 等添加的视图。如果这些数据可能会导致性能大幅下降您从作曲家分享的内容是从数据库中提取的。

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-01-20
    • 2021-04-16
    • 2019-12-06
    • 2021-09-15
    • 2018-06-17
    • 1970-01-01
    相关资源
    最近更新 更多