php – 在许多关系中命名表格
作者:互联网
我关注多对多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的更多信息.
标签:laravel-migrations,php,laravel,laravel-5,eloquent 来源: https://codeday.me/bug/20191005/1856840.html