Laravel - Paginate和get()

时间:2013-10-27 09:50:10

标签: php pagination laravel laravel-4

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

我尝试了什么:

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

}

2 个答案:

答案 0 :(得分:22)

如果查看method signature,您会看到paginate收到第二个参数$ columns。所以你的解决方案是使用

->paginate($this->limit, $this->fields);

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

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

答案 1 :(得分:1)

class Servicios extends CI_Controller
{

    public function __construct()
    {

        parent::__construct();

        header('Content-Type: application/json');
        if (!$this->lib_validaciones->validarSesion(FALSE))
        {
            exit(json_encode(array("satisfactorio" => FALSE, "mensaje" => "NO TIENE SESSION ACTIVA")));
        }
        $this->usuarioId = $this->session->userdata("usuarioId");
    }

    public function index()
    {
        exit();
    }

    public function getPremios()
    {
        $currentPage = $this->input->get("pag");

        \Illuminate\Pagination\Paginator::currentPageResolver(function () use ($currentPage)
        {
            return $currentPage;
        });

        $this->load->model('Premio');

        $premios = Premio::where('activo', "TRUE")
                ->with(['Categoria' => function($q)
                    {
                        $q->select('id', 'nombre');
                    }])
                ->with(['Imagenes' => function ($query)
                    {
                        $query->where("activo", "TRUE");
                        $query->select(["imagenes.id", "imagenes.descripcion",
                            new Illuminate\Database\Query\Expression(
                                    "CONCAT('" . site_url(PATH_IMAGENES_UPLOAD) . "',imagenes.id,'.',imagenes.extension) as path")
                        ]);
                    }])
                ->with(['inventario'])
                ->withCount(['Favoritos', 'Favoritos AS favorito_usuario' => function ($query)
                    {
                        $query->where("usuario_id", $this->usuarioId);
                    }])
                ->orderBy("nombre")
                ->paginate(3);

        $premios->setPath(site_url(uri_string()));
        $premios->setPageName("pag");

        exit(json_encode(array("satisfactorio" => TRUE, "premios" => $premios->toArray())));
    }

}