我担心多对多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');
}
本文地址:http://php.askforanswer.com/zaiduoduiduoguanxizhongmingmingbiao.html
文章标签:eloquent , laravel , laravel-5 , laravel-migrations , php
版权声明:本文为原创文章,版权归 admin 所有,欢迎分享本文,转载请保留出处!
文章标签:eloquent , laravel , laravel-5 , laravel-migrations , php
版权声明:本文为原创文章,版权归 admin 所有,欢迎分享本文,转载请保留出处!
评论已关闭!