2016-01-20 23 views
5

多対多ラーベル関係で自動命名表について検討しました。例えば多くの関係の命名表laravel

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

ときに、テーブル名を変更します。私は私の関係でエラーが発生している

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

product_featureとは何ですか?

+0

正確なエラーとは何ですか?また、どのデータベースを使用していますか? –

答えて

15

Laravelのピボットテーブルの命名規則は、アンダースコアで区切られたアルファベット順の単一のテーブル名です。したがって、あるテーブルがfeaturesで、もう1つのテーブルがproductsの場合、ピボットテーブルはfeature_productになります。

任意のテーブル名(たとえば、product_feature)を自由に使用できますが、関係にピボットテーブルの名前を指定する必要があります。これは、belongsToMany()関数の2番目のパラメータを使用して行われます。

// 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'); 
}