Ped*_*ndi 27 php laravel eloquent laravel-5 laravel-migrations
我关注多对多Laravel关系中的自动命名表.
例如:
Schema::create('feature_product', function (Blueprint $table) {
Run Code Online (Sandbox Code Playgroud)
将表名更改为:
Schema::create('product_feature', function (Blueprint $table) {
Run Code Online (Sandbox Code Playgroud)
我的关系有误.
有什么问题product_feature
?
pat*_*cus 61
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');
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
16363 次 |
最近记录: |