【问题标题】:Laravel - Paginating Eloquent Retrieved resultsLaravel - 分页 Eloquent 检索结果
【发布时间】:2013-01-30 15:49:31
【问题描述】:

这就是我的控制器里的东西

$projects=Project::where_sup_id($user_id)->get();
   $total=count($projects);
   $per_page=3;
  $projects = Paginator::make($projects, $total, $per_page);
  return View::make('project.index')->with('projects',$projects);

这在我看来

 @foreach ($projects->results as $project) 

      {{$project->title}}

 @endforeach


{{$projects->links();}}

但是当我在浏览器中查看它时,它会显示所有页面中的所有行...链接显示完美!你认为是什么问题?请帮忙!提前谢谢你!

【问题讨论】:

  • 修复了错误,然后又出现了另一个错误——不要忘记->results 的必要性,否则你会得到一个模棱两可的Trying to get property of non-object 错误

标签: pagination laravel eloquent


【解决方案1】:

您正在计算所有行,但根本不使用 limit,Laravel 流畅的查询构建器有 skip()take() 方法。

顺便说一句,您不需要手动对其进行分页。 Laravel 使用 paginate() 方法自动进行分页。

这样做:

$projects = Project::where_sup_id($user_id)->paginate(3); // 3 means records per page
return View::make('project.index')->with('projects', $projects);

您正在做view 正常工作。

【讨论】:

    猜你喜欢
    • 2015-07-17
    • 2018-12-12
    • 2017-02-24
    • 2017-01-12
    • 1970-01-01
    • 2021-03-24
    • 2015-03-26
    • 2018-01-17
    • 2016-06-15
    相关资源
    最近更新 更多