Codeigniter 3.1.4 with Eloquent 5.4 Service

0

Controller Services to get AWARDS including count and paged

This is my service controller, with CODEIGNITER 3.4 and ELOQUENT 5.4

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())));
}
    
asked by Marcelo Secaira 12.05.2017 в 19:41
source

1 answer

0

Answer: {   "satisfactory": true,   "awards": {     "total": 12,     "per_page": 3,     "current_page": 2,     "last_page": 4,     "next_page_url": " link ",     "prev_page_url": " link ",     "from": 4,     "to": 6,     "data": [       {         "id": 4,         "active": "TRUE",         "label": "NO",         "name": "TICKETS TO THE MOVIE",         "description": "",         "required points": 300,         "category_id": 4,         "created_a": "2017-05-11 09:20:27",         "updated_a": null,         "Favorites_count": 0,         "book_user_favorite": 0,         "category": {           "id": 4,           "name": "OTHERS"         },         "images": [],         "inventory": null       },       {         "id": 9,         "active": "TRUE",         "label": "NO",         "name": "JUICE EXTRACTOR FPSTJE317S",         "description": "NONE",         "required points": 1200,         "category_id": 1,         "created_a": "2017-05-05 16:02:54",         "updated_a": null,         "Favorites_count": 0,         "book_user_favorite": 0,         "category": {           "id": 1,           "name": "OSTER PRODUCTS"         },         "images": [],         "inventory": null       },       {         "id": 1,         "active": "TRUE",         "label": "NO",         "name": "BLENDER 6455",         "description": "NONE",         "required points": 1000,         "category_id": 1,         "created_a": "2017-05-05 16:02:54",         "updated_a": null,         "Favorites_count": 2,         "book_user_favorite": 1,         "category": {           "id": 1,           "name": "OSTER PRODUCTS"         },         "images": [           {             "id": 1,             "description": "OSTER BLENDER",             "path": " link ",             "pivot": {               "prize_id": 1,               "image_id": 1             }           }         ],         "Inventory": {           "prize_id": 1,           "balance": "10"         }       }     ]   } }

    
answered by 12.05.2017 в 19:41