Advertisement
Gxbriel_99

laravel fk

Apr 15th, 2025
30
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.09 KB | None | 0 0
  1. Schema::create('contatto_indirizzi', function (Blueprint $table) {
  2. $table->id();
  3. $table->unsignedBigInteger('idContatto'); // Relazione con la tabella dei contatti
  4. $table->string('via', 255); // Via dell'indirizzo
  5. $table->string('civico', 3); // Numero civico
  6. $table->string('scala', 50)->nullable(); // Scala (opzionale)
  7. $table->string('interno', 50)->nullable(); // Interno (opzionale)
  8. $table->string('cap', 5); // CAP
  9. $table->unsignedBigInteger('idComune'); // Relazione con la tabella dei comuni
  10. $table->string('provincia', 100); // Provincia
  11. $table->string('regione', 100); // Regione
  12. $table->string('stato', 100)->nullable(); // Stato (opzionale)
  13. $table->timestamps();
  14.  
  15. // Definizione delle chiavi esterne
  16. $table->foreign('idContatto')->references('idContatto')->on('contatto');
  17. $table->foreign('idComune')->references('idComune')->on('comuni');
  18. });
  19.  
  20. //---------------//
  21.  
  22. <?php
  23.  
  24. namespace App\Models;
  25.  
  26. use Illuminate\Database\Eloquent\Factories\HasFactory;
  27. use Illuminate\Database\Eloquent\Model;
  28.  
  29. class contattoIndirizzi extends Model
  30. {
  31. use HasFactory;
  32. protected $table = 'contatto_indirizzi';
  33. protected $primaryKey = 'id';
  34.  
  35. protected $fillable = [
  36. 'idContatto',
  37. 'via',
  38. 'civico',
  39. 'scala',
  40. 'interno',
  41. 'cap',
  42. 'idComune',
  43. 'provincia',
  44. 'regione',
  45. 'stato'
  46. ];
  47.  
  48. public function contatto()
  49. {
  50. return $this->belongsTo(Contatto::class, 'idContatto');
  51. }
  52. }
  53.  
  54. //----------------------------//
  55. //----------------------------//
  56. <?php
  57.  
  58. use Illuminate\Database\Migrations\Migration;
  59. use Illuminate\Database\Schema\Blueprint;
  60. use Illuminate\Support\Facades\Schema;
  61.  
  62. return new class extends Migration
  63. {
  64. /**
  65. * Run the migrations.
  66. */
  67. public function up(): void
  68. {
  69. Schema::create('comuni', function (Blueprint $table) {
  70. $table->id('idComune');
  71. $table->string('Codice_ISTAT')->unique();
  72. $table->string('nome_Comune', 100);
  73. $table->string('regione', 100);
  74. $table->string('citta', 35);
  75. $table->string('provincia', 100);
  76. $table->string('cap', 5);
  77. $table->timestamps();
  78. });
  79. }
  80.  
  81. /**
  82. * Reverse the migrations.
  83. */
  84. public function down(): void
  85. {
  86. Schema::dropIfExists('comuni');
  87. }
  88. };
  89. //----------------------//
  90.  
  91.  
  92. <?php
  93.  
  94. namespace App\Models;
  95.  
  96. use Illuminate\Database\Eloquent\Factories\HasFactory;
  97. use Illuminate\Database\Eloquent\Model;
  98.  
  99. class Comuni extends Model
  100. {
  101. use HasFactory;
  102.  
  103. protected $table = 'comuni';
  104. protected $primaryKey = 'idComune';
  105.  
  106. protected $fillable = [
  107. 'Codice_ISTAT',
  108. 'nome_Comune',
  109. 'regione',
  110. 'citta',
  111. 'provincia',
  112. 'cap',
  113. ];
  114. }
  115.  
Tags: laravel
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement