Loading app/Http/Controllers/DependenciaController.php 0 → 100644 +86 −0 Original line number Diff line number Diff line <?php namespace App\Http\Controllers; use App\Models\Dependencia; use Illuminate\Http\Request; class DependenciaController extends Controller { /** * Display a listing of the resource. */ public function index() { $dependencias = Dependencia::get(['id', 'nombre']); return view('adminGen.catalogos.dependencias', ['dependencia' => $dependencias]); } /** * Show the form for creating a new resource. */ public function create() { // } /** * Store a newly created resource in storage. */ public function store(Request $request) { $validated = $request->validate([ 'nombre' => 'required|max:150', ]); $dependencia = new Dependencia; $dependencia->nombre = $request->nombre; $dependencia->save(); return redirect()->route('catalogos.dependencias.get')->with('success', 'Dependencia creada correctamente.'); } /** * Display the specified resource. */ public function show(Dependencia $dependencia) { // } /** * Show the form for editing the specified resource. */ public function edit(Dependencia $dependencia) { // } /** * Update the specified resource in storage. */ public function update(Request $request, Dependencia $dependencia) { $validated = $request->validate([ 'id' => 'required:min:1', 'nombre' => 'required|max:150', ]); $dependencia = Dependencia::find($request->id); $dependencia->nombre = $request->nombre; $dependencia->save(); return redirect()->route('catalogos.dependencias.get')->with('success', 'Dependencia actualizada correctamente.'); } /** * Remove the specified resource from storage. */ public function destroy($id) { try{ $dependencia = Dependencia::findOrFail($id); $dependencia->delete(); return redirect()->route('catalogos.dependencias.get')->with('success', 'Dependencia eliminada correctamente.'); }catch(\Exception $e){ return redirect()->route('catalogos.dependencias.get')->withErrors('Error al eliminar la dependencia:' . $e->getMessage()); } } } app/Http/Middleware/CheckRoles.php 0 → 100644 +32 −0 Original line number Diff line number Diff line <?php namespace App\Http\Middleware; use Closure; use Illuminate\Http\Request; use Symfony\Component\HttpFoundation\Response; class CheckRoles { /** * Handle an incoming request. * * @param \Closure(\Illuminate\Http\Request): (\Symfony\Component\HttpFoundation\Response) $next */ public function handle(Request $request, Closure $next, ... $roles): Response { if($roles){ $user = auth()->user(); foreach($roles as $role){ if($user->hasRole($role)){ return $next($request); } } return abort(401); }else{ return $next($request); } } } app/Models/Dependencia.php 0 → 100644 +24 −0 Original line number Diff line number Diff line <?php namespace App\Models; use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Relations\HasMany; use App\Models\User; class Dependencia extends Model { use HasFactory; protected $primaryKey = 'id'; protected $fillable = [ 'nombre', ]; public function users(): HasMany { return $this->hasMany(User::class); } } bootstrap/app.php +2 −0 Original line number Diff line number Diff line Loading @@ -5,6 +5,7 @@ use Illuminate\Foundation\Configuration\Middleware; use App\Http\Middleware\CheckBanned; use App\Http\Middleware\CheckRoles; return Application::configure(basePath: dirname(__DIR__)) ->withRouting( Loading @@ -15,6 +16,7 @@ ) ->withMiddleware(function (Middleware $middleware) { $middleware->append(CheckBanned::class); $middleware->append(CheckRoles::class); }) ->withExceptions(function (Exceptions $exceptions) { // Loading database/migrations/2024_06_10_154242_create_dependencias_table.php 0 → 100644 +34 −0 Original line number Diff line number Diff line <?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('dependencias', function (Blueprint $table) { $table->id(); $table->string('nombre'); $table->timestamps(); }); Schema::table('users', function(Blueprint $table){ $table->unsignedBigInteger('dependencia_id'); $table->foreign('dependencia_id')->references('id')->on('dependencias'); }); } /** * Reverse the migrations. */ public function down(): void { Schema::dropIfExists('dependencias'); } }; Loading
app/Http/Controllers/DependenciaController.php 0 → 100644 +86 −0 Original line number Diff line number Diff line <?php namespace App\Http\Controllers; use App\Models\Dependencia; use Illuminate\Http\Request; class DependenciaController extends Controller { /** * Display a listing of the resource. */ public function index() { $dependencias = Dependencia::get(['id', 'nombre']); return view('adminGen.catalogos.dependencias', ['dependencia' => $dependencias]); } /** * Show the form for creating a new resource. */ public function create() { // } /** * Store a newly created resource in storage. */ public function store(Request $request) { $validated = $request->validate([ 'nombre' => 'required|max:150', ]); $dependencia = new Dependencia; $dependencia->nombre = $request->nombre; $dependencia->save(); return redirect()->route('catalogos.dependencias.get')->with('success', 'Dependencia creada correctamente.'); } /** * Display the specified resource. */ public function show(Dependencia $dependencia) { // } /** * Show the form for editing the specified resource. */ public function edit(Dependencia $dependencia) { // } /** * Update the specified resource in storage. */ public function update(Request $request, Dependencia $dependencia) { $validated = $request->validate([ 'id' => 'required:min:1', 'nombre' => 'required|max:150', ]); $dependencia = Dependencia::find($request->id); $dependencia->nombre = $request->nombre; $dependencia->save(); return redirect()->route('catalogos.dependencias.get')->with('success', 'Dependencia actualizada correctamente.'); } /** * Remove the specified resource from storage. */ public function destroy($id) { try{ $dependencia = Dependencia::findOrFail($id); $dependencia->delete(); return redirect()->route('catalogos.dependencias.get')->with('success', 'Dependencia eliminada correctamente.'); }catch(\Exception $e){ return redirect()->route('catalogos.dependencias.get')->withErrors('Error al eliminar la dependencia:' . $e->getMessage()); } } }
app/Http/Middleware/CheckRoles.php 0 → 100644 +32 −0 Original line number Diff line number Diff line <?php namespace App\Http\Middleware; use Closure; use Illuminate\Http\Request; use Symfony\Component\HttpFoundation\Response; class CheckRoles { /** * Handle an incoming request. * * @param \Closure(\Illuminate\Http\Request): (\Symfony\Component\HttpFoundation\Response) $next */ public function handle(Request $request, Closure $next, ... $roles): Response { if($roles){ $user = auth()->user(); foreach($roles as $role){ if($user->hasRole($role)){ return $next($request); } } return abort(401); }else{ return $next($request); } } }
app/Models/Dependencia.php 0 → 100644 +24 −0 Original line number Diff line number Diff line <?php namespace App\Models; use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Relations\HasMany; use App\Models\User; class Dependencia extends Model { use HasFactory; protected $primaryKey = 'id'; protected $fillable = [ 'nombre', ]; public function users(): HasMany { return $this->hasMany(User::class); } }
bootstrap/app.php +2 −0 Original line number Diff line number Diff line Loading @@ -5,6 +5,7 @@ use Illuminate\Foundation\Configuration\Middleware; use App\Http\Middleware\CheckBanned; use App\Http\Middleware\CheckRoles; return Application::configure(basePath: dirname(__DIR__)) ->withRouting( Loading @@ -15,6 +16,7 @@ ) ->withMiddleware(function (Middleware $middleware) { $middleware->append(CheckBanned::class); $middleware->append(CheckRoles::class); }) ->withExceptions(function (Exceptions $exceptions) { // Loading
database/migrations/2024_06_10_154242_create_dependencias_table.php 0 → 100644 +34 −0 Original line number Diff line number Diff line <?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('dependencias', function (Blueprint $table) { $table->id(); $table->string('nombre'); $table->timestamps(); }); Schema::table('users', function(Blueprint $table){ $table->unsignedBigInteger('dependencia_id'); $table->foreign('dependencia_id')->references('id')->on('dependencias'); }); } /** * Reverse the migrations. */ public function down(): void { Schema::dropIfExists('dependencias'); } };