diff --git a/app/Models/Barrio.php b/app/Models/Barrio.php index 9c48da6..fd1a0c7 100644 --- a/app/Models/Barrio.php +++ b/app/Models/Barrio.php @@ -31,4 +31,12 @@ public function pedidos(): HasMany { return $this->hasMany(Pedido::class); } + + /** + * Los productos que pertenecen al barrio. + */ + public function productos(): HasMany + { + return $this->hasMany(Producto::class); + } } diff --git a/app/Models/Producto.php b/app/Models/Producto.php index 6c3e62d..92593de 100644 --- a/app/Models/Producto.php +++ b/app/Models/Producto.php @@ -13,7 +13,7 @@ class Producto extends Model * @var array */ protected $fillable = [ - 'name', 'price', 'solidario', 'bono', 'categoria_id' + 'name', 'price', 'solidario', 'bono', 'categoria_id', 'barrio_id' ]; /** @@ -39,4 +39,12 @@ public function caracteristicas(): BelongsToMany { return $this->belongsToMany(Caracteristica::class); } + + /** + * El barrio a la que pertenece el producto. + */ + public function barrio(): BelongsTo + { + return $this->belongsTo(Barrio::class); + } } diff --git a/database/migrations/2024_03_11_224041_create_productos_table.php b/database/migrations/2024_03_11_224041_create_productos_table.php index 139a9a6..272592d 100644 --- a/database/migrations/2024_03_11_224041_create_productos_table.php +++ b/database/migrations/2024_03_11_224041_create_productos_table.php @@ -18,8 +18,10 @@ public function up(): void $table->unsignedBigInteger('categoria_id'); $table->boolean('solidario'); $table->boolean('bono'); + $table->unsignedBigInteger('barrio_id')->nullable(); $table->timestamps(); $table->foreign('categoria_id')->references('id')->on('categorias'); + $table->foreign('barrio_id')->references('id')->on('barrios'); }); }