在学习到laravel框架创建分页时出现了运行错误。php
控制器ArticleController的代码html
public function index() { $data=Article::orderBy('art_id','desc')->Paginate(2); return view('admin.article.index',compact('data')); }
在视图index.blade.php中的代码laravel
<div class="page_list"> {{$data->links()}} </div>
运行报错浏览器
Method links does not exist. (View: D:\AppServ\www\mylaravel\resources\views\admin\article\index.blade.php)
参考http://stackoverflow.com/ques...app
Method links does not exist. (View: C:\xampp\htdocs\app\resources\views\search.blade.php)
CONTROLLER框架
$posts = Post::where('visible', 1) ->where('expire_date', '>', $current)->where('delete', 0); $posts->paginate(1); $posts = $posts->get(); return view('search', compact('posts'));
VIEWpost
<div class="pagination-bar text-center"> {{ $posts->links() }} </div>
Answer学习
Change you code to this:this
$posts = Post::where('visible', 1) ->where('expire_date', '>', $current) ->where('delete', 0) ->paginate(1); return view('search', compact('posts'));
Your code doesn't work because you do not save paginate() results to a variable, like $posts = $posts->paginate(1);. Also, you shouldn't use get() or all() after paginate().url
看的似懂非懂的,实际上是不懂的!
又查看laravel技术文档 http://laravelacademy.org/pos...
尝试该换simplePaginate方法来实现,结果仍是不行!
其实用dd($data);向浏览器输出时,能够提取出页码元素。控制器部分应该时没有问题的。只是抱着一丝但愿试试,失败!
又将index.blade.php中的改成
<div class="page_list"> {!! $data->render() !!} </div>
运行时经过,不过仅仅出现了
觉得这样可能时bug致使。就没有继续修改,毕竟深层原理不知道。
更重要的是,在技术文档中三、在视图中显示分页结果中阐述simplePaginate与paginate时,没有在该部分明确说明其中的差异。
继续看[ Laravel 5.1 文档 ] 服务 —— 分页部分,发如今3.3 更多帮助方法中有
$results->count()
$results->currentPage()
$results->hasMorePages()
$results->lastPage() (使用simplePaginate时无效)
$results->nextPageUrl()
$results->perPage()
$results->total() (使用simplePaginate时无效)
$results->url($page)
使用simplepaginate无效的方法,遂就查看ArticleController中时paginete仍是simplePaginate;发现时simplePaginate,将其改成paginate。再次运行。达到效果!
具体缘由未知,待继续深刻学习后爬代码看结果。