To determine the table name of the relationship's intermediate table, Eloquent will join the two related model names in alphabetical order.
This would mean a join between Post and Tag could be added like this:
class Post extends Model{ public $table = 'posts'; public function tags() { return $this->belongsToMany(Tag::class); }}
However, you are free to override this convention, and you would need to specify the join table in the second argument.
class Post extends Model{ public $table = 'posts'; public function tags() { return $this->belongsToMany(Tag::class, 'posts_tags'); }}
If you wish to be explicit about the primary keys you can also supply these as third and fourth arguments.
class Post extends Model{ public $table = 'posts'; public function tags() { return $this->belongsToMany(Tag::class, 'post_tag', 'post_id', 'tag_id'); }}
Tip given by @iammikek
Enjoyed This Tip?
Get access to all premium tutorials, video and text courses, and exclusive Laravel resources. Join our community of 10,000+ developers.
Recent Courses
Laravel Coding with AI Agents: Cursor, Claude Code, Codex
5 lessons
1 h 01 min
PhpStorm Junie AI for Laravel Projects: Crash Course
7 lessons
36 min
Laravel HTTP Client and 3rd-Party APIs
7 lessons
50 min