Los eventos se crean con un slug. Ejecutar migraciones al traer este commit por primera vez.

This commit is contained in:
nat 2021-12-28 18:07:01 -03:00
parent b2de025915
commit af437b315d
3 changed files with 45 additions and 0 deletions

View File

@ -48,6 +48,7 @@ class EventoController extends Controller
]); ]);
$evento = new Evento(); $evento = new Evento();
$evento->nombre = $request->input("nombre"); $evento->nombre = $request->input("nombre");
$evento->setSlug();
$evento->save(); $evento->save();
return redirect()->route("edit", ['evento' => $evento]); return redirect()->route("edit", ['evento' => $evento]);
} }

View File

@ -4,6 +4,7 @@ namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Model;
use Str;
class Evento extends Model class Evento extends Model
{ {
@ -54,4 +55,15 @@ class Evento extends Model
} }
return ($dur_a > $dur_b) ? -1 : 1; 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++;
}
}
} }

View File

@ -0,0 +1,32 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class EventoSlug extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::table('eventos', function (Blueprint $table) {
$table->string('slug')->unique();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::table('eventos', function (Blueprint $table) {
$table->dropColumn('slug');
});
}
}