Laravel Eloquent Relationships explained

Asked by Avatar Beginner User 105 • 1 month ago • 181 views
Backend Development
1
Login to vote
Can someone explain the difference between hasMany and belongsTo in Laravel? I'm confused about when to use which.
Solved

2 Answers

6
To add to that, the belongsTo side is always where the foreign key column is stored in the database. So if your posts table has a user_id column, then Post belongsTo User.
Answered by Avatar David Okonkwo 910 • 1 month ago
Best Answer Marked by question author
7
Great question! Think of it like this:

- **belongsTo**: The model with the foreign key. Example: A Post `belongsTo` a User (because posts table has user_id)

- **hasMany**: The opposite side. Example: A User `hasMany` Posts (because one user can have multiple posts)

So on the Post model: `belongsTo(User::class)`
On the User model: `hasMany(Post::class)`
Answered by Avatar John Doe 1470 • 1 month ago
Please login to post an answer.
Question Stats
  • Asked: Jan 21, 2026
  • Views: 181
  • Votes: 1
  • Answers: 2
Avatar
Beginner User

Frontend Development

105 Rep Intermediate
Member since Jan 2026