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'); + }); + } +}