Laravel - Paginate和get()

Baj*_*kie 11 php pagination laravel laravel-4

使用下面的代码,我想要的是对我创建的查询进行分页.但是,当我尝试在get之后添加paginate时,会抛出错误.我想保持得到,因为我想限制在$ fields上设置的列.分页这个东西应该是更好的想法?或者什么是获取和限制列的良好替代品?

我尝试了什么:

->get($this->fields)->paginate($this->limit)
Run Code Online (Sandbox Code Playgroud)

我控制器的一部分:

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);
    }
Run Code Online (Sandbox Code Playgroud)

}

Wil*_*ley 22

如果查看方法签名,您将看到paginate收到第二个参数$ columns.所以你的解决方案就是使用

->paginate($this->limit, $this->fields);
Run Code Online (Sandbox Code Playgroud)

此外,您可以通过稍微改变一下来清理控制器:

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);
}
Run Code Online (Sandbox Code Playgroud)