Laravel-分页和get() [英] Laravel - Paginate and get()
本文介绍了Laravel-分页和get()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
使用下面的代码,我想要的是对我创建的查询进行分页.但是,当我尝试在获取后添加分页时,会引发错误.我想保持获取,因为我想限制在$ fields上设置的列. 对这个东西进行分页应该是更好的主意吗?还是获取并限制列的好替代方法?
With the code below, what I wanted was paginate the query I created. But, when I try to add paginate after get, it throws an error. I wanted to remain get since I want to limit to columns that was set on $fields. What would should be the better idea to paginate this thing? or what's a good substitute for get and limit the columns?
我尝试过的事情:
->get($this->fields)->paginate($this->limit)
我的控制器的一部分:
class PhonesController extends BaseController {
protected $limit = 5;
protected $fields = array('Phones.*','manufacturers.name as manufacturer');
/**
* Display a listing of the resource.
*
* @return Response
*/
public function index()
{
if (Request::query("str")) {
$phones = Phone::where("model", 'LIKE', '%'. Request::query('str') . '%')
->join('manufacturers', 'manufacturers_id', '=', 'manufacturers.id')
->get($this->fields);
} else {
$phones = Phone::join('manufacturers', 'manufacturers_id', '=', 'manufacturers.id')
->get($this->fields);
}
return View::make('phones.index')->with('phones', $phones);
}
}
推荐答案
If you look at the method signature you will see that paginate receives a second argument, $columns. So your solution would be to use
->paginate($this->limit, $this->fields);
此外,您可以通过稍作更改来清理控制器:
Furthermore, you can clean up your controller by changing things slightly:
public function index()
{
$query = Phones::join('manufacturers', 'manufacturers_id', '=', 'manufacturers.id');
if ( Request::query('str') ) {
$query->where('model', 'LIKE', '%'. Request::query('str') . '%')
}
$phones = $query->paginate($this->limit, $this->fields);
return view('phones.index')->with('phones', $phones);
}
这篇关于Laravel-分页和get()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文