How Eloquent determines the foreign key of the relationship?

Posted 9 months ago by CDNxL

The documentation says:

Eloquent determines the foreign key of the relationship based on the model name.

but in the HasRelationship class in the belongsTo method, I can see this comment

If no foreign key was supplied, we can use a backtrace to guess the proper foreign key name by using the name of the relationship function, which when combined with an "_id" should conventionally match the columns.

So.. the documentation is wrong?

I've tested it and is how it works exactly? (I already know how to set it manually)


Please sign in or create an account to participate in this conversation.

Laracasts Mascot

Hi, Have We Met Yet?

Did you know that, in addition to the forum, Laracasts includes well over 1000 lessons on modern web development? All for the price of one lunch out per month.

Sign Me Up


Reply to

Use Markdown with GitHub-flavored code blocks.