Laravel - Paginate and get()

前端 未结 2 1617
鱼传尺愫
鱼传尺愫 2021-01-04 08:15

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 li

2条回答
  •  孤城傲影
    2021-01-04 08:37

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

提交回复
热议问题