Skip to content

Instantly share code, notes, and snippets.

@goshmx
Created May 9, 2017 21:11
Show Gist options
  • Save goshmx/7f3a4e7afb9c58575df2aa0c1855dd73 to your computer and use it in GitHub Desktop.
Save goshmx/7f3a4e7afb9c58575df2aa0c1855dd73 to your computer and use it in GitHub Desktop.
Laravel catalogo
<?php
class CatalogoController extends \BaseController {
public $respuesta = array(
'status' => true,
'msg' => ""
);
public $statusHttp = 200;
public $tabla;
public function __construct()
{
if(!Schema::hasTable(Input::get('tipo'))){
$this->statusHttp = 400;
$this->respuesta['status'] = false;
$this->respuesta['msg'] = "La tabla es requerida o no existe";
http_response_code(400);
header('Content-Type: application/json');
die(json_encode($this->respuesta));
}
else{
$this->tabla = Input::get('tipo');
if(Config::get('app.debug')){
$this->respuesta['tabla'] = $this->tabla;
$this->respuesta['campos'] = Input::all();
$this->respuesta['columnas'] = Schema::getColumnListing($this->tabla);
}
}
}
/**
* Display a listing of the resource.
*
* @return Response
*/
public function index()
{
$paginar = Input::get('paginate');
$pagina = (Input::get('pagina'))?Input::get('pagina'):false;
$rowsPerPage = (Input::get('porPagina'))?Input::get('porPagina'):10;
if($paginar){
$registros = DB::table($this->tabla)->skip(($pagina*$rowsPerPage))->take($rowsPerPage)->orderBy('id', 'asc')->get();
$this->respuesta['pagina'] = $pagina;
$this->respuesta['rows_per_page'] = $rowsPerPage;
}
else{
$registros = DB::table($this->tabla)->orderBy('id', 'asc')->get();
}
$this->respuesta['data'] = $registros;
if(count($registros)>0){
$this->respuesta['msg'] = "Listado de registros";
}
else{
$this->respuesta['msg'] = "No se encontraron registros disponibles";
}
return Response::json($this->respuesta,$this->statusHttp);
}
/**
* Store a newly created resource in storage.
*
* @return Response
*/
public function store()
{
$campos = Input::except('tabla','id','updated_at','created_at');
$dataSet = $this->generaCampos(Schema::getColumnListing($this->tabla),$campos);
if(count($dataSet)>0){
try {
$queryInsertId = DB::table($this->tabla)->insertGetId($dataSet);
$this->respuesta['id'] = $queryInsertId;
$this->respuesta['msg'] = "Los datos han sido insertados.";
}
catch (\Exception $e) {
$this->statusHttp = 400;
$this->respuesta['status'] = false;
$this->respuesta['msg'] = "Datos invalidos";
}
}
else{
$this->statusHttp = 400;
$this->respuesta['status'] = false;
$this->respuesta['msg'] = "No hay campos a insertar en la BD";
}
return Response::json($this->respuesta,$this->statusHttp);
}
/**
* Display the specified resource.
*
* @param int $id
* @return Response
*/
public function show($id)
{
$registro = DB::table($this->tabla)->where('id', $id)->first();
if($registro){
$this->respuesta['msg'] = "Datos obtenidos de la BD";
$this->respuesta['data'] = $registro;
}
else{
$this->respuesta['status'] = false;
$this->respuesta['msg'] = "El registro no existe en la BD";
}
return Response::json($this->respuesta,$this->statusHttp);
}
/**
* Update the specified resource in storage.
*
* @param int $id
* @return Response
*/
public function update($id)
{
$campos = Input::except('tabla','id','updated_at','created_at');
$dataSet = $this->generaCampos(Schema::getColumnListing($this->tabla),$campos);
if(count($dataSet)>0){
try {
$queryUpdated = DB::table($this->tabla)
->where('id', $id)
->update($dataSet);
$this->respuesta['id'] = $id;
$this->respuesta['msg'] = "Los datos han sido actualizados.";
}
catch (\Exception $e) {
$this->statusHttp = 400;
$this->respuesta['msg'] = "Datos invalidos";
}
}
else{
$this->statusHttp = 400;
$this->respuesta['msg'] = "No hay campos a actualizar en la BD";
}
return Response::json($this->respuesta,$this->statusHttp);
}
/**
* Remove the specified resource from storage.
*
* @param int $id
* @return Response
*/
public function destroy($id)
{
$registro = DB::table($this->tabla)->where('id', $id)->delete();
if($registro){
$this->respuesta['id'] = $id;
$this->respuesta['msg'] = "El registro ha sido eliminado.";
}
else{
$this->statusHttp = 400;
$this->respuesta['msg'] = "El registro no existe en la BD";
}
return Response::json($this->respuesta,$this->statusHttp);
}
private function generaCampos($columnas,$campos){
$dataSet = [];
foreach($columnas as $columna){
if(($columna != 'id') || ($columna != 'created_at') || ($columna != 'updated_at')){
if (array_key_exists($columna,$campos)){
$dataSet[$columna] = $campos[$columna];
}
}
}
return $dataSet;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment