如何关联 laravel 中的三个表
How to relate three tables in laravel
我正在尝试在 laravel 中开发一个 p2p 应用程序。
我的数据库中有三个表,即用户、贷款和分期付款。
现在,用户和贷款可以有多个分期付款,每个分期付款都属于一个用户和一个贷款。
那么我该如何定义这种关系呢?我需要多态关系吗?如果是这样,如何将用户和贷款与单笔分期付款联系起来?
each installment will belong to a user and a loan.
这几乎回答了这个问题。
如果您的要求是“每期付款将属于一个用户或一笔贷款”,则关系将是多态的。
最小table结构
Schema::create('users', function (Blueprint $table) {
$table->bigIncrements('id');
... // other users fields or indexes.
});
Schema::create('loans', function (Blueprint $table) {
$table->bigIncrements('id');
... // other loans fields or indexes.
});
Schema::create('installments', function (Blueprint $table) {
$table->bigIncrements('id');
$table->foreignId('user_id')->constrained('users');
$table->foreignId('loan_id')->constrained('loans');
... // other installments fields or indexes.
});
如果 Installment 的 user_id
或 loan_id
可以为空,请在 ->constrained()
之前添加 ->nullable()
。例如:
Schema::create('installments', function (Blueprint $table) {
...
$table->foreignId('user_id')->nullable()->constrained('users');
...
});
如果同一 user/loan 对不能有多个分期付款,请添加一个唯一索引。
Schema::create('installments', function (Blueprint $table) {
...
$table->unique(['user_id', 'loan_id']);
...
});
要定义的关系
用户模型:
public function installments() { return $this->hasMany(Installment::class); }
public function loans() { return $this->belongsToMany(Loan::class, 'installments'); }
贷款模式:
public function installments() { return $this->hasMany(Installment::class); }
public function users() { return $this->belongsToMany(User::class, 'installments'); }
分期付款模式:
public function loan() { return $this->belongsTo(Loan::class); }
public function user() { return $this->belongsTo(User::class); }
https://laravel.com/docs/eloquent-relationships#inserting-and-updating-related-models
https://laravel.com/docs/eloquent-relationships#syncing-associations
https://laravel.com/docs/eloquent-relationships#updating-a-record-on-the-intermediate-table
如果您有唯一约束,syncWithoutDetaching()
和 updateExistingPivot()
是特别重要的方法。
我正在尝试在 laravel 中开发一个 p2p 应用程序。
我的数据库中有三个表,即用户、贷款和分期付款。
现在,用户和贷款可以有多个分期付款,每个分期付款都属于一个用户和一个贷款。
那么我该如何定义这种关系呢?我需要多态关系吗?如果是这样,如何将用户和贷款与单笔分期付款联系起来?
each installment will belong to a user and a loan.
这几乎回答了这个问题。
如果您的要求是“每期付款将属于一个用户或一笔贷款”,则关系将是多态的。
最小table结构
Schema::create('users', function (Blueprint $table) {
$table->bigIncrements('id');
... // other users fields or indexes.
});
Schema::create('loans', function (Blueprint $table) {
$table->bigIncrements('id');
... // other loans fields or indexes.
});
Schema::create('installments', function (Blueprint $table) {
$table->bigIncrements('id');
$table->foreignId('user_id')->constrained('users');
$table->foreignId('loan_id')->constrained('loans');
... // other installments fields or indexes.
});
如果 Installment 的 user_id
或 loan_id
可以为空,请在 ->constrained()
之前添加 ->nullable()
。例如:
Schema::create('installments', function (Blueprint $table) {
...
$table->foreignId('user_id')->nullable()->constrained('users');
...
});
如果同一 user/loan 对不能有多个分期付款,请添加一个唯一索引。
Schema::create('installments', function (Blueprint $table) {
...
$table->unique(['user_id', 'loan_id']);
...
});
要定义的关系
用户模型:
public function installments() { return $this->hasMany(Installment::class); }
public function loans() { return $this->belongsToMany(Loan::class, 'installments'); }
贷款模式:
public function installments() { return $this->hasMany(Installment::class); }
public function users() { return $this->belongsToMany(User::class, 'installments'); }
分期付款模式:
public function loan() { return $this->belongsTo(Loan::class); }
public function user() { return $this->belongsTo(User::class); }
https://laravel.com/docs/eloquent-relationships#inserting-and-updating-related-models
https://laravel.com/docs/eloquent-relationships#syncing-associations
https://laravel.com/docs/eloquent-relationships#updating-a-record-on-the-intermediate-table
如果您有唯一约束,syncWithoutDetaching()
和 updateExistingPivot()
是特别重要的方法。