Laravel 抛出:方法 paginate 不存在错误
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/42382893/
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 throwing: method paginate does not exist error
提问by Mikethetechy
I am trying to use pagination in laravel. so i try to retrieve all the transactions and paginate it. my flow goes like this for your reference view->controller->repository->model .
我正在尝试在 Laravel 中使用分页。所以我尝试检索所有交易并对其进行分页。我的流程是这样的,供您参考 view->controller->repository->model 。
myrepository:
我的存储库:
public function getall(){
$this->transaction = Transaction::all();
return $this->transaction;
}
mycontroller:
我的控制器:
public function getall(){
$transactions = $this->transaction->getall()->paginate(10);
//dd($this->transaction);
return view('transactions', ['transactions' => $transactions]);
}
in my app.php under service providers i made sure i have pagination: Illuminate\Pagination\PaginationServiceProvider::class,
在我的 app.php 下服务提供者我确保我有分页:Illuminate\Pagination\PaginationServiceProvider::class,
but there is no aliases though. so in my controller i did : use Illuminate\Pagination;
但是没有别名。所以在我的控制器中我做了:使用 Illuminate\Pagination;
回答by geckob
It won't work with your way. Because all
method will give your Collection
. Paginate function only works on Eloquent\Builder
or Eloquent Model
它不会按照你的方式工作。因为all
方法会给你的Collection
. 分页功能仅适用于Eloquent\Builder
或Eloquent Model
If you need to paginate all records without condition,
如果您需要无条件对所有记录进行分页,
\App\Transaction::paginate(10);
回答by EddyTheDove
Do like this
这样做
//in repository
public function getAll($limit)
{
$this->transaction = Transaction::paginate($limit); //Use paginate here.
return $this->transaction;
}
//in controller
public function getall() {
$transactions = $this->transaction->getall(10);
}
回答by Saravanan Sampathkumar
Add the following function to your repository
将以下函数添加到您的存储库
Repository
存储库
public function getModel() {
return new Transaction;
}
public function getAll() {
return $this->getModel()->all();
}
public function paginate($limit = 15) {
return $this->getModel()->paginate($limit);
}
Controller
控制器
public function getAll() {
$transaction = $this->transaction->paginate(10);
return view('transactions', ['transactions' => $transactions]);
}
回答by benjamintemitope
If you want to used orderBy()
and paginate()
如果你想使用orderBy()
和paginate()
Used something like this
使用了这样的东西
$transactions = Transaction::all();
$transactions = Transaction::orderBy('name')->paginate(10);
return view('index', compact('transactions'));