2

我正在尝试在 laravel 中使用分页。所以我尝试检索所有交易并将其分页。我的流程是这样的,供您参考 view->controller->repository->model 。

我的仓库:

public function getall(){

    $this->transaction = Transaction::all();


    return $this->transaction;

}

我的控制器:

 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;

4

4 回答 4

5

它不会以你的方式工作。因为all方法会给你的Collection. 分页功能仅适用于Eloquent\BuilderEloquent Model

如果您需要无条件对所有记录进行分页,

\App\Transaction::paginate(10);
于 2017-02-22T04:51:01.013 回答
2

这样做

//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);

}   
于 2017-02-22T04:55:10.973 回答
1

将以下函数添加到您的存储库

存储库

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]);
}
于 2017-02-22T04:57:28.533 回答
0

如果你想使用orderBy()paginate()

用过这样的东西

 $transactions = Transaction::all();

 $transactions = Transaction::orderBy('name')->paginate(10);

 return view('index', compact('transactions'));
于 2019-01-31T00:54:27.983 回答