Los eventos se crean con un slug. Ejecutar migraciones al traer este commit por primera vez.
This commit is contained in:
		
							parent
							
								
									b2de025915
								
							
						
					
					
						commit
						af437b315d
					
				
					 3 changed files with 45 additions and 0 deletions
				
			
		|  | @ -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]); | ||||
|     } | ||||
|  |  | |||
|  | @ -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++; | ||||
|         } | ||||
|     } | ||||
| } | ||||
|  |  | |||
							
								
								
									
										32
									
								
								database/migrations/2021_12_28_205423_evento_slug.php
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										32
									
								
								database/migrations/2021_12_28_205423_evento_slug.php
									
										
									
									
									
										Normal 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'); | ||||
|         }); | ||||
|     } | ||||
| } | ||||
		Loading…
	
	Add table
		
		Reference in a new issue
	
	 nat
						nat