Browse Source

Capacidades V1

main
Damian 3 weeks ago
parent
commit
11501fae62
  1. 92
      app/Http/Controllers/CapacidadController.php
  2. 13
      app/Models/Capacidad.php
  3. 24
      database/factories/CapacidadFactory.php
  4. 28
      database/migrations/2025_03_13_133629_create_capacidads_table.php
  5. 21
      database/seeders/CapacidadSeeder.php
  6. 66
      resources/views/capacidades.blade.php
  7. 132
      resources/views/capacidadesCrearEditar.blade.php
  8. 4
      resources/views/layouts/dashboard.blade.php
  9. 2
      routes/web.php

92
app/Http/Controllers/CapacidadController.php

@ -0,0 +1,92 @@
<?php
namespace App\Http\Controllers;
use App\Models\Capacidad;
use Illuminate\Http\Request;
class CapacidadController extends Controller
{
/**
* Display a listing of the resource.
*/
public function index(Request $request)
{
$busqueda = $request->busqueda;
if($busqueda) {
$capacidades = Capacidad::where('cantidad', 'LIKE', "%{$busqueda}%")->get();
if($capacidades->count() == 0) {
return redirect()->route('capacidades.index')
->with('error', 'No existe ninguna capacidad con la cantidad "' . $busqueda . '". Por favor, inténtalo de nuevo.');
}
if($capacidades->count() == 1) {
$capacidad = $capacidades->first();
return redirect()->route('capacidades.edit', $capacidad->id);
}
return view('capacidades', ["capacidades" => $capacidades]);
}
$capacidades = Capacidad::all();
return view('capacidades', ["capacidades" => $capacidades]);
}
/**
* Show the form for creating a new resource.
*/
public function create()
{
return view('capacidadesCrearEditar');
}
/**
* Store a newly created resource in storage.
*/
public function store(Request $request)
{
$capacidad = new Capacidad($request->all());
$capacidad->save();
return redirect()->route('capacidades.index')->with('success', 'Capacidad creada exitosamente.');
}
/**
* Display the specified resource.
*/
public function show(Capacidad $capacidad)
{
//
}
/**
* Show the form for editing the specified resource.
*/
public function edit($id)
{
$capacidad = Capacidad::find($id);
return view('capacidadesCrearEditar', ['capacidad' => $capacidad]);
}
/**
* Update the specified resource in storage.
*/
public function update(Request $request, $id)
{
$capacidad = Capacidad::find($id);
$capacidad->fill($request->all());
$capacidad->save();
return redirect()->route('capacidades.index')->with('success', 'Capacidad actualizada exitosamente.');
}
/**
* Remove the specified resource from storage.
*/
public function destroy($id)
{
$capacidad = Capacidad::find($id);
$capacidad->delete();
return redirect()->route('capacidades.index')->with('success', 'Capacidad eliminada exitosamente.');
}
}

13
app/Models/Capacidad.php

@ -0,0 +1,13 @@
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class Capacidad extends Model
{
use HasFactory;
protected $table = 'capacidades';
protected $fillable = ['capacidad'];
}

24
database/factories/CapacidadFactory.php

@ -0,0 +1,24 @@
<?php
namespace Database\Factories;
use Illuminate\Database\Eloquent\Factories\Factory;
/**
* @extends \Illuminate\Database\Eloquent\Factories\Factory<\App\Models\Capacidad>
*/
class CapacidadFactory extends Factory
{
/**
* Define the model's default state.
*
* @return array<string, mixed>
*/
public function definition(): array
{
return [
//
];
}
}

28
database/migrations/2025_03_13_133629_create_capacidads_table.php

@ -0,0 +1,28 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
/**
* Run the migrations.
*/
public function up(): void
{
Schema::create('capacidades', function (Blueprint $table) {
$table->id();
$table->integer('capacidad')->unique();
$table->timestamps();
});
}
/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::dropIfExists('capacidades');
}
};

21
database/seeders/CapacidadSeeder.php

@ -0,0 +1,21 @@
<?php
namespace Database\Seeders;
use Illuminate\Database\Console\Seeds\WithoutModelEvents;
use Illuminate\Database\Seeder;
use App\Models\Capacidad;
class CapacidadSeeder extends Seeder
{
/**
* Run the database seeds.
*/
public function run(): void
{
Capacidad::create([
'capacidad' => 100,
]);
//
}
}

66
resources/views/capacidades.blade.php

@ -0,0 +1,66 @@
@extends('layouts.dashboard')
@section('content')
<div class="container mx-auto px-4 py-6">
@if(session('success'))
<div id="success-message" class="bg-green-100 border border-green-400 text-green-700 px-4 py-3 rounded relative mb-4" role="alert">
<span class="block sm:inline">{{ session('success') }}</span>
</div>
@endif
@if(session('error'))
<div class="bg-red-100 border border-red-400 text-red-700 px-4 py-3 rounded relative mb-4" role="alert">
<span class="block sm:inline">{{ session('error') }}</span>
</div>
@endif
<div class="bg-white rounded-lg shadow-lg">
<div class="p-4 border-b border-gray-200 flex justify-between items-center">
<h2 class="text-2xl font-bold">Gestión de Capacidades</h2>
<a href="{{ route('capacidades.create') }}"
class="px-4 py-2 bg-blue-500 text-white rounded-lg hover:bg-blue-600 flex items-center gap-2">
<i class="fas fa-plus"></i>
Agregar
</a>
</div>
<div class="overflow-x-auto">
<table class="min-w-full divide-y divide-gray-200">
<thead class="bg-gray-50">
<tr>
<th class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">ID</th>
<th class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">Cantidad</th>
<th class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">Acciones</th>
</tr>
</thead>
<tbody class="bg-white divide-y divide-gray-200">
@foreach($capacidades as $capacidad)
<tr class="hover:bg-gray-50">
<td class="px-6 py-4 whitespace-nowrap text-sm text-gray-500">{{ $capacidad->id }}</td>
<td class="px-6 py-4 whitespace-nowrap text-sm text-gray-900">{{ $capacidad->cantidad }}</td>
<td class="px-6 py-4 whitespace-nowrap text-sm">
<div class="flex gap-2">
<a href="{{ route('capacidades.edit', $capacidad->id) }}"
class="text-blue-600 hover:text-blue-900">
<i class="fas fa-edit"></i>
</a>
<form action="{{ route('capacidades.destroy', $capacidad->id) }}"
method="POST"
class="inline">
@csrf
@method('DELETE')
<button type="submit"
class="text-red-600 hover:text-red-900">
<i class="fas fa-trash"></i>
</button>
</form>
</div>
</td>
</tr>
@endforeach
</tbody>
</table>
</div>
</div>
</div>
@endsection

132
resources/views/capacidadesCrearEditar.blade.php

@ -0,0 +1,132 @@
@extends('layouts.dashboard')
@section('content')
<div class="container mx-auto px-4 py-6">
<div class="max-w-lg mx-auto">
<div class="bg-white rounded-lg shadow-lg overflow-hidden">
<div class="p-6">
<!-- Encabezado del formulario -->
<div class="flex items-center justify-between mb-6">
<h2 class="text-2xl font-bold text-gray-800">
{{ isset($capacidad) ? 'Editar Capacidad' : 'Nueva Capacidad' }}
</h2>
<div class="h-10 w-10 bg-blue-100 rounded-full flex items-center justify-center">
<i class="fas fa-database text-blue-600"></i>
</div>
</div>
<!-- Mensajes de error -->
@if($errors->any())
<div class="mb-6 bg-red-50 border-l-4 border-red-500 p-4 rounded-r-lg">
<div class="flex items-center">
<i class="fas fa-exclamation-circle text-red-500 mr-3"></i>
<div class="text-red-700">
<ul>
@foreach($errors->all() as $error)
<li>{{ $error }}</li>
@endforeach
</ul>
</div>
</div>
</div>
@endif
<!-- Formulario -->
<form id="capacidadForm"
action="{{ isset($capacidad) ? route('capacidades.update', $capacidad->id) : route('capacidades.store') }}"
method="POST">
@csrf
@if(isset($capacidad))
@method('PUT')
@endif
<div class="space-y-6">
<!-- Campo Cantidad -->
<div>
<label for="cantidad" class="block text-sm font-medium text-gray-700 mb-2">
Cantidad
</label>
<div class="relative rounded-md shadow-sm">
<div class="absolute inset-y-0 left-0 pl-3 flex items-center pointer-events-none">
<i class="fas fa-hashtag text-gray-400"></i>
</div>
<input type="number"
name="cantidad"
id="cantidad"
class="block w-full pl-10 pr-3 py-2 border border-gray-300 rounded-md focus:ring-2 focus:ring-blue-500 focus:border-blue-500"
value="{{ isset($capacidad) ? $capacidad->cantidad : old('cantidad') }}"
placeholder="Ingrese la cantidad"
min="0"
required>
</div>
@error('cantidad')
<p class="mt-1 text-sm text-red-600">{{ $message }}</p>
@enderror
</div>
<!-- Botones de acción -->
<div class="flex justify-end space-x-2 pt-4 border-t border-gray-200">
<a href="{{ route('capacidades.index') }}"
class="px-4 py-2 border border-gray-300 rounded-md text-sm font-medium text-gray-700 hover:bg-gray-50 focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-blue-500">
Cancelar
</a>
<button type="submit"
class="px-4 py-2 border border-transparent rounded-md shadow-sm text-sm font-medium text-white bg-blue-600 hover:bg-blue-700 focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-blue-500">
{{ isset($capacidad) ? 'Actualizar' : 'Guardar' }}
</button>
</div>
</div>
</form>
</div>
</div>
</div>
</div>
<script>
function confirmarAccion() {
const esEdicion = {{ isset($capacidad) ? 'true' : 'false' }};
const cantidad = document.getElementById('cantidad').value.trim();
if (!cantidad) {
Swal.fire({
title: 'Error',
text: 'La cantidad es obligatoria',
icon: 'error',
confirmButtonColor: '#3085d6',
confirmButtonText: 'Entendido'
});
return;
}
Swal.fire({
title: esEdicion ? '¿Editar capacidad?' : '¿Guardar capacidad?',
html: esEdicion ?
`¿Estás seguro de editar la capacidad a:<br><strong>${cantidad}</strong>?` :
`¿Estás seguro de guardar la capacidad:<br><strong>${cantidad}</strong>?`,
icon: 'question',
showCancelButton: true,
confirmButtonColor: '#3085d6',
cancelButtonColor: '#d33',
confirmButtonText: esEdicion ? 'Sí, editar' : 'Sí, guardar',
cancelButtonText: 'Cancelar'
}).then((result) => {
if (result.isConfirmed) {
document.getElementById('capacidadForm').submit();
}
});
}
// Prevenir envío del formulario con Enter
document.getElementById('capacidadForm').addEventListener('keypress', function(e) {
if (e.key === 'Enter') {
e.preventDefault();
confirmarAccion();
}
});
// Focus en el campo cantidad al cargar la página
document.addEventListener('DOMContentLoaded', function() {
document.getElementById('cantidad').focus();
});
</script>
@endsection

4
resources/views/layouts/dashboard.blade.php

@ -113,8 +113,8 @@
<!-- Capacidad -->
<li>
<a href="/configuracion/capacidad" class="nav-item-hover flex items-center space-x-3 px-4 py-3 rounded-lg hover:bg-white/10 backdrop-blur-sm">
<i class="fas fa-users text-white/80"></i>
<a href="{{ route('capacidades.index') }}" class="nav-item-hover flex items-center space-x-3 px-4 py-3 rounded-lg hover:bg-white/10 backdrop-blur-sm">
<i class="fas fa-database text-white/80"></i>
<span class="font-light">Capacidad</span>
</a>
</li>

2
routes/web.php

@ -6,6 +6,7 @@ use App\Http\Controllers\HomeController;
use App\Http\Controllers\MarcaController;
use App\Http\Controllers\DocentesController;
use App\Http\Controllers\TiposVeiculosController;
use App\Http\Controllers\CapacidadController;
/*
|--------------------------------------------------------------------------
| Web Routes
@ -26,6 +27,7 @@ Auth::routes(['register'=>true,'reset'=>false]);
Route::resource('marca', MarcaController::class);
Route::resource('docentes', DocentesController::class);
Route::resource('vehiculos', TiposVeiculosController::class);
Route::resource('capacidades', CapacidadController::class);
// Rutas protegidas que requieren autenticación

Loading…
Cancel
Save