Merge branch 'feature/barrios-y-regiones' into feature/modelos-y-migraciones

This commit is contained in:
Alejandro Tasistro 2024-03-12 16:40:08 -03:00
commit 61fcb2853e
6 changed files with 147 additions and 6 deletions

26
app/Models/Barrio.php Normal file
View File

@ -0,0 +1,26 @@
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class Barrio extends Model
{
/**
* The attributes that are mass assignable.
*
* @var array<int, string>
*/
protected $fillable = [
'name',
];
/**
* La región a la que pertenece el barrio.
*/
public function region(): BelongsTo
{
return $this->belongsTo(Region::class);
}
}

33
app/Models/Region.php Normal file
View File

@ -0,0 +1,33 @@
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class Region extends Model
{
/**
* The table associated with the model.
*
* @var string
*/
protected $table = 'regiones';
/**
* The attributes that are mass assignable.
*
* @var array<int, string>
*/
protected $fillable = [
'name',
];
/**
* Los barrios que pertenecen a la región.
*/
public function barrios(): HasMany
{
return $this->hasMany(Barrio::class);
}
}

View File

@ -0,0 +1,28 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
/**
* Run the migrations.
*/
public function up(): void
{
Schema::create('regiones', function (Blueprint $table) {
$table->id();
$table->string('name', 100);
$table->timestamps();
});
}
/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::dropIfExists('regiones');
}
};

View File

@ -0,0 +1,30 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
/**
* Run the migrations.
*/
public function up(): void
{
Schema::create('barrios', function (Blueprint $table) {
$table->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');
}
};

View File

@ -0,0 +1,27 @@
<?php
namespace Database\Seeders;
use Illuminate\Support\Facades\Date;
use Illuminate\Support\Facades\DB;
use Illuminate\Database\Console\Seeds\WithoutModelEvents;
use Illuminate\Database\Seeder;
class BarrioSeeder extends Seeder
{
/**
* Run the database seeds.
*/
public function run(): void
{
DB::table('regiones')->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()],
]);
}
}

View File

@ -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,
]);
}
}