Laravel throwing: method paginate does not exist error
16,242
Solution 1
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);
}
Solution 2
It won't work with your way. Because all
method will give your Collection
. Paginate function only works on Eloquent\Builder
or Eloquent Model
If you need to paginate all records without condition,
\App\Transaction::paginate(10);
Solution 3
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]);
}
Comments
-
Manas almost 2 years
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 .
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,
but there is no aliases though. so in my controller i did : use Illuminate\Pagination;