I have multiple tables, and some do have columns which are related but I am not certain if I have to put the foreign key relations to all columns which are related.
Here are some of my migrations schemas.
users
Schema::create('users', function (Blueprint $table) {
$table->id();
$table->string('name');
$table->string('email')->unique();
$table->boolean('role')->nullable( );
$table->timestamp('email_verified_at')->nullable();
$table->string('password');
$table->rememberToken()->nullable();
$table->timestamps();
});
bids
Schema::create('bids', function (Blueprint $table) {
$table->id();
$table->unsignedBigInteger('loan_id');
$table->unsignedBigInteger('user_id');
$table->decimal('interest');
$table->string('PayType');
$table->integer('IntervalPay');
$table->string('GracePeriod');
$table->timestamps();
$table->foreign('user_id')
->references('id')->on('users')->ondelete('cascade');
$table->foreign('loan_id')
->references('id')->on('loan_request')->ondelete('cascade');
});
loan_contracts
Schema::create('loan_contracts', function (Blueprint $table) {
$table->id();
$table->unsignedBigInteger('lender_id');
$table->unsignedBigInteger('borrower_id');
$table->integer('LoanType');
$table->Biginteger('amount');
$table->decimal('interest');
$table->string('GracePeriod');
$table->string('PayType');
$table->integer('IntervalPay');
$table->timestamps();
});
loan_request
Schema::create('loan_request', function (Blueprint $table) {
$table->id();
$table->unsignedBigInteger('users_id');
$table->integer('LoanType');
$table->Biginteger('amount');
$table->string('PayType');
$table->integer('IntervalPay');
$table->string('GracePeriod');
$table->timestamps();
$table->foreign('users_id')
->references('id')->on('users')->ondelete('cascade');
});
If you observe you will see common column names.