Laravel 5.2 分页
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/34991182/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Laravel 5.2 paginating
提问by Fadee
I used this way to make a pagination for my site, but I still get an error! I tried to solve and I searched a lot, didn't find a solution. I hope you can help me.
我用这种方式为我的网站制作了分页,但我仍然收到错误消息!我试图解决,我搜索了很多,没有找到解决方案。我希望你能帮助我。
Controller -
控制器 -
class ContentController extends MasterController {
public function content() {
$content = content::all()->paginate(10);
$content->setPath('content'); //Customise Page Url
return view('content.boot',compact('content'));
}
}
view -
看法 -
@extends('master')
@section('content')
@if(count($content) > 0 )
@foreach($content as $row)
<video width="330" controls>
<source src="{{ asset('videos/' . $row['video'] )}}" type="video/mp4">
</video>
@endforeach
@endif
{!! $content->render() !!}
@endsection
route -
路线 -
Route::get('/', 'ContentController@content');
Error -
错误 -
BadMethodCallException in Macroable.php line 81:
Method paginate does not exist.
Macroable.php 第 81 行中的 BadMethodCallException:
方法 paginate 不存在。
回答by Gouda Elalfy
remove all() function, your code should be:
删除 all() 函数,您的代码应该是:
$content = content::paginate(10);
回答by Kdecherf
As suggested by Gouda Elalfy you should remove the call to all()
.
根据 Gouda Elalfy 的建议,您应该删除对all()
.
Explanation
解释
The method paginate()
is available on Eloquent\Builder
which is what you implicitly have when you call content::paginage(10)
.
该方法paginate()
是可用的,Eloquent\Builder
当您调用content::paginage(10)
.
However content::all()
returns a Collection
or an array of Model
, not a Builder
.
但是content::all()
返回一个Collection
或一个数组Model
,而不是一个Builder
。
回答by H.Kontodios
Here it explains how to do it https://laravel.com/docs/5.2/paginationand based on that you should do:
1) In your controller change the line
$content = content::all()->paginate(10);
to be
$content = content::paginate(10);
2) In your view you could use this
{{ $content->appends(Request::except('page'))->links() }}
This will do what you want!!
在这里它解释了如何做https://laravel.com/docs/5.2/pagination并基于此你应该做的:
1)在你的控制器中更改行 $content = content::all()->pagination(10 );
成为
$content = content::paginate(10);
2) 在你看来你可以使用这个
{{ $content->appends(Request::except('page'))->links() }}
这会做你想做的!!