【发布时间】:2017-07-22 04:42:39
【问题描述】:
我有一个用户表和一个“地址”表。结账时可以输入多个地址供选择。
现在我希望能够将一个地址设置为标准发票地址,并将另一个地址设置为标准发票地址。我考虑过使用数据透视表,其中包含以下内容:
Schema::create('users_adresses_pivot', function (Blueprint $table) {
$table->increments('id');
$table->integer('user_id')->unsigned();
$table->foreign('user_id')->references('id')->on('users');
$table->integer('invoice_adress_id')->unsigned();
$table->foreign('invoice_adress_id')->references('id')->on('adresses');
$table->integer('delivery_adress_id')->unsigned();
$table->foreign('delivery_adress_id')->references('id')->on('adresses');
$table->timestamps();
});
但是我不确定这是否正确。对我来说,这看起来有点像一个不好的做法。如果我选择此选项,我该如何设置关系?因为我需要从用户到地址的两个关系。我已经得到了一个,以便在视图中浏览它们。因此,如果我用 Pivot Relation 替换它,我只能获得标准发票和交货地址,而不是其他地址。有什么想法吗?
这是我目前的关系: 用户.php:
public function adresses()
{
return $this->hasMany('App\Adress');
}
地址.php:
public function user()
{
return $this->belongsTo('App\User');
}
【问题讨论】:
标签: laravel pivot-table relationship