Laravel - Paginate and get()

安稳与你 提交于 2019-12-18 19:23:08

问题


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?

What I tried:

->get($this->fields)->paginate($this->limit)

Part of my controller:

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

}


回答1:


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



回答2:


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

}


来源:https://stackoverflow.com/questions/19616720/laravel-paginate-and-get

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!