When Excluding Table Names from Conventions in Laravel Many-to-Many Relationships

When designing many-to-many relationships, I thought I was following the documentation correctly, but I made a small misunderstanding.

Here are three tables, right?

The tables this time:

  • events
  • event_tags
  • event_tag_event ← pivot table

Normally, the tables would be:

  • events
  • tags
  • tag_event

If you follow the default conventions, you can create relationships like this. However, when you use slightly unconventional names, there are some things to watch out for.

Checking the documentation

Laravel 5.1 Eloquent: Relationships

Oh, I see, you just need to provide the second argument.

When I checked using tinker...

It throws an error. ヽ(´ー`)ノ

The second argument is the pivot table name!

Could it be that I need to specify the pivot table name?

SQLSTATE[42000]: Syntax error or access violation: 1066 Not unique table/alias on relationship

No more errors. ヽ(´ー`)ノ

Thoughts

Recently, I’ve been more interested in frontend than backend, and I can’t sleep at night. ヽ(´ー`)ノ