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;
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);
}
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);
Add the following function to your repository
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]);
}
If you want to used orderBy()
and paginate()
Used something like this
$transactions = Transaction::all();
$transactions = Transaction::orderBy('name')->paginate(10);
return view('index', compact('transactions'));