Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Izak00's avatar
Level 2

Laravel pivot table naming convention

Hello, I’m having trouble deciding on the naming convention for pivot tables when using Eloquent, particularly when dealing with specific table names.

Here’s the scenario:

I have a products table and a categories table. The pivot table for these would traditionally be named category_product. However, I also have specific tables for different types of products, such as glasses_products.

Similarly, instead of a generic categories table, I use glasses_categories for this type of product. Given these table names, what would be the correct name for the pivot table joining glasses_products and glasses_categories?

Would it follow the convention of two singular table names separated by an underscore, resulting in glasses_product_glasses_category, or is there a better way to name it?

Thanks!

0 likes
11 replies
LaryAI's avatar
Level 58

In Laravel, the convention for naming pivot tables is to use the singular form of the related table names, ordered alphabetically and separated by an underscore. However, when dealing with specific table names like glasses_products and glasses_categories, you can follow a similar approach but adapt it to your specific naming needs.

For your scenario, you have two specific tables: glasses_products and glasses_categories. The pivot table should ideally reflect the relationship between these two tables. A good naming convention would be to use the singular form of each specific table name, ordered alphabetically.

In this case, the singular forms would be glasses_product and glasses_category. Alphabetically, glasses_category comes before glasses_product, so the pivot table name would be:

glasses_category_glasses_product

However, if you find this name too verbose or if it doesn't fit well with your application's naming conventions, you can choose a more concise name that still clearly represents the relationship, such as glasses_category_product.

Remember, Laravel's Eloquent allows you to specify the pivot table name explicitly in your relationship methods if you deviate from the default naming convention. You can do this using the ->using() method or by specifying the table name directly in the belongsToMany method:

public function glassesProducts()
{
    return $this->belongsToMany(GlassesProduct::class, 'glasses_category_glasses_product');
}

This way, you maintain clarity in your database schema while still adhering to Laravel's flexibility.

Tray2's avatar

There is a convention, and it states that you should have the table names in singular form and in alphabetical order.

  • Users
  • Posts

Would become post_user

2 likes
Izak00's avatar
Level 2

@Tray2 That I understand and it seems simple when it regards a one-word table, it confusing when it comes to joining "glasses_categories" and "glasses_products", so following the convention the pivot table would be "glasses_category_glasses_product".

martinbean's avatar

That I understand and it seems simple when it regards a one-word table, it confusing when it comes to joining "glasses_categories" and "glasses_products", so following the convention the pivot table would be "glasses_category_glasses_product".

@Izak00 It doesn’t matter. The convention still applies. So yes, the pivot table would indeed be named glasses_category_glasses_product

1 like
Snapey's avatar

dont get hung up on it, you can always overide convention.

Personally I would probably go for glasses_product_category

But then again I would not have created diffferent product tables in the first place

1 like
Izak00's avatar
Level 2

@Snapey Thank you for the insight! I would love to omit creating specific product tables, but the app I am creating needs a separation between glasses and lenses. Each has different category structure and it is difficult to create a single products table for them.

reaz's avatar

For this kind of specific structure, Its better to come up with your own rule and follow them in through out the codebase. You can always mention the specific table name, while declaring the pivot relation in the model. But from you example, isn't glasses_categories should be glass_categories according Laravel convention?

1 like
Izak00's avatar
Level 2

@reaz It makes sense to come up with a custom rule, I tried to stick to the convention too much. According to convention, the pivot table should use singular form for each entity, but the glasses_categories table is not a pivot table, it represent a type of a product, glasses (eyewear). I need a separation as there is another type of products called lenses, which will have it's own category structure.

Please or to participate in this conversation.