我关注多对多Laravel关系中的自动命名表.
例如:
Schema::create('feature_product', function (Blueprint $table) {
将表名更改为:
Schema::create('product_feature', function (Blueprint $table) {
我的关系有误.
product_feature有什么问题?
解决方法:
Laravel对数据透视表的命名约定是基于蛇形的模型名称,按字母顺序用下划线分隔.因此,如果一个模型是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');
}
您可以阅读有关many to many relationships in the docs的更多信息.
原文地址:https://codeday.me/bug/20191005/1856840.html
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。