繁体   English   中英

在多对多关系 laravel 中命名表

[英]naming tables in many to many relationships laravel

我关心多对多 Laravel 关系中的自动命名表。

例如:

Schema::create('feature_product', function (Blueprint $table) {}

当将表名更改为:

Schema::create('product_feature', function (Blueprint $table) {}

我的关系有误。

product_feature怎么了?

Laravel 对数据透视表的命名约定是 snake_cased 模型名称,按字母顺序排列,下划线分隔。 因此,如果一个模型是Feature ,而另一个模型是Product ,则数据透视表将为feature_product

您可以随意使用任何您想要的表名(例如product_feature ),但您随后需要在关系中指定数据透视表的名称。 这是使用belongsToMany()函数的第二个参数完成的。

// in Product model
public function features()
{
    return $this->belongsToMany('App\Feature', 'product_feature');
}

// in Feature model
public function products()
{
    return $this->belongsToMany('App\Product', 'product_feature');
}

您可以在文档中阅读更多关于多对多关系的信息。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM