diff --git a/app/Models/Barrio.php b/app/Models/Barrio.php new file mode 100644 index 0000000..02384cc --- /dev/null +++ b/app/Models/Barrio.php @@ -0,0 +1,26 @@ + + */ + protected $fillable = [ + 'name', + ]; + + /** + * La región a la que pertenece el barrio. + */ + public function region(): BelongsTo + { + return $this->belongsTo(Region::class); + } +} diff --git a/app/Models/Region.php b/app/Models/Region.php new file mode 100644 index 0000000..53ddecb --- /dev/null +++ b/app/Models/Region.php @@ -0,0 +1,33 @@ + + */ + protected $fillable = [ + 'name', + ]; + + /** + * Los barrios que pertenecen a la región. + */ + public function barrios(): HasMany + { + return $this->hasMany(Barrio::class); + } +} diff --git a/database/migrations/2024_03_11_205603_create_regions_table.php b/database/migrations/2024_03_11_205603_create_regions_table.php new file mode 100644 index 0000000..8417e86 --- /dev/null +++ b/database/migrations/2024_03_11_205603_create_regions_table.php @@ -0,0 +1,28 @@ +id(); + $table->string('name', 100); + $table->timestamps(); + }); + } + + /** + * Reverse the migrations. + */ + public function down(): void + { + Schema::dropIfExists('regiones'); + } +}; diff --git a/database/migrations/2024_03_11_210402_create_barrios_table.php b/database/migrations/2024_03_11_210402_create_barrios_table.php new file mode 100644 index 0000000..8d1352e --- /dev/null +++ b/database/migrations/2024_03_11_210402_create_barrios_table.php @@ -0,0 +1,30 @@ +id(); + $table->string('name', 100); + $table->unsignedBigInteger('region_id'); + $table->foreign('region_id')->references('id')->on('regiones'); + $table->timestamps(); + }); + } + + /** + * Reverse the migrations. + */ + public function down(): void + { + Schema::dropIfExists('barrios'); + } +}; diff --git a/database/seeders/BarrioSeeder.php b/database/seeders/BarrioSeeder.php new file mode 100644 index 0000000..595f278 --- /dev/null +++ b/database/seeders/BarrioSeeder.php @@ -0,0 +1,27 @@ +insert([ + ['name'=>'PRUEBA', 'created_at'=>Date::now()] + ]); + + $prueba_id = DB::table('regiones')->where('name','PRUEBA')->first()->id; + + DB::table('barrios')->insert([ + ['name'=>'PRUEBA','region_id'=>$prueba_id, 'created_at'=>Date::now()], + ]); + } +} diff --git a/database/seeders/DatabaseSeeder.php b/database/seeders/DatabaseSeeder.php index a9f4519..9e2405c 100644 --- a/database/seeders/DatabaseSeeder.php +++ b/database/seeders/DatabaseSeeder.php @@ -12,11 +12,8 @@ class DatabaseSeeder extends Seeder */ public function run(): void { - // \App\Models\User::factory(10)->create(); - - // \App\Models\User::factory()->create([ - // 'name' => 'Test User', - // 'email' => 'test@example.com', - // ]); + $this->call([ + BarrioSeeder::class, + ]); } }