[英]Laravel throwing: method paginate does not exist error
我試圖在laravel中使用分頁。 所以我嘗試檢索所有的交易並對其進行分頁。 我的流程就像你的參考視圖 - > 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]);
}
在我的app.php服務提供商下我確定我有分頁:Illuminate \\ Pagination \\ PaginationServiceProvider :: class,
但是沒有別名。 所以在我的控制器中我做了:使用Illuminate \\ Pagination;
它不適合你的方式。 因為all
方法都會給你的Collection
。 Paginate函數僅適用於Eloquent\\Builder
或Eloquent Model
如果您需要在沒有條件的情況下對所有記錄進行分頁,
\App\Transaction::paginate(10);
這樣做
//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);
}
將以下函數添加到存儲庫
public function getModel() {
return new Transaction;
}
public function getAll() {
return $this->getModel()->all();
}
public function paginate($limit = 15) {
return $this->getModel()->paginate($limit);
}
public function getAll() {
$transaction = $this->transaction->paginate(10);
return view('transactions', ['transactions' => $transactions]);
}
如果你想使用orderBy()
和paginate()
使用這樣的東西
$transactions = Transaction::all();
$transactions = Transaction::orderBy('name')->paginate(10);
return view('index', compact('transactions'));
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.