From af437b315dfef4b4ea7f821f44c72f6e6f5674f8 Mon Sep 17 00:00:00 2001 From: nat Date: Tue, 28 Dec 2021 18:07:01 -0300 Subject: [PATCH] Los eventos se crean con un slug. Ejecutar migraciones al traer este commit por primera vez. --- app/Http/Controllers/EventoController.php | 1 + app/Models/Evento.php | 12 +++++++ .../2021_12_28_205423_evento_slug.php | 32 +++++++++++++++++++ 3 files changed, 45 insertions(+) create mode 100644 database/migrations/2021_12_28_205423_evento_slug.php diff --git a/app/Http/Controllers/EventoController.php b/app/Http/Controllers/EventoController.php index bea301a..a1a0f9a 100644 --- a/app/Http/Controllers/EventoController.php +++ b/app/Http/Controllers/EventoController.php @@ -48,6 +48,7 @@ class EventoController extends Controller ]); $evento = new Evento(); $evento->nombre = $request->input("nombre"); + $evento->setSlug(); $evento->save(); return redirect()->route("edit", ['evento' => $evento]); } diff --git a/app/Models/Evento.php b/app/Models/Evento.php index 034b158..7ffa7a7 100644 --- a/app/Models/Evento.php +++ b/app/Models/Evento.php @@ -4,6 +4,7 @@ namespace App\Models; use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Model; +use Str; class Evento extends Model { @@ -54,4 +55,15 @@ class Evento extends Model } return ($dur_a > $dur_b) ? -1 : 1; } + + //Llamado liego de la validación. Asume un nombre válido. + public function setSlug() + { + $baseSlug = Str::slug($this->nombre); + $this->slug = $baseSlug; + $i = 1; + while (self::whereSlug($this->slug)->exists()){ + $this->slug = $baseSlug . "-" . $i++; + } + } } diff --git a/database/migrations/2021_12_28_205423_evento_slug.php b/database/migrations/2021_12_28_205423_evento_slug.php new file mode 100644 index 0000000..947c452 --- /dev/null +++ b/database/migrations/2021_12_28_205423_evento_slug.php @@ -0,0 +1,32 @@ +string('slug')->unique(); + }); + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + Schema::table('eventos', function (Blueprint $table) { + $table->dropColumn('slug'); + }); + } +}