naming tables in many to many relationships laravel naming tables in many to many relationships laravel laravel laravel

naming tables in many to many relationships laravel


Laravel's naming convention for pivot tables is snake_cased model names in alphabetical order separated by an underscore. So, if one model is Feature, and the other model is Product, the pivot table will be feature_product.

You are free to use any table name you want (such as product_feature), but you will then need to specify the name of the pivot table in the relationship. This is done using the second parameter to the belongsToMany() function.

// in Product modelpublic function features(){    return $this->belongsToMany('App\Feature', 'product_feature');}// in Feature modelpublic function products(){    return $this->belongsToMany('App\Product', 'product_feature');}

You can read more about many to many relationships in the docs.