One-to-One, One-to-Many, and Many-to-Many Relationships
Real-world data is rarely isolated — a user has one profile, a post has many comments, and a product can belong to many categories at once. Eloquent models these connections directly, so related data can be accessed almost as naturally as a regular property.
One-to-One
A one-to-one relationship links exactly one record on each side, such as a user and their profile. It's defined using the hasOne() method on the owning model, paired with belongsTo() on the related model.
One-to-Many
A one-to-many relationship connects a single record to multiple related records, like a single author with many blog posts. This is one of the most common relationship types in everyday applications.
Many-to-Many
A many-to-many relationship allows records on both sides to relate to multiple records on the other, such as students enrolling in multiple courses while each course has multiple students. This type requires an intermediate table to track the connections.
| Relationship | Eloquent Method | Example |
|---|---|---|
| One-to-One | hasOne() / belongsTo() | User ↔ Profile |
| One-to-Many | hasMany() / belongsTo() | Author → Posts |
| Many-to-Many | belongsToMany() | Students ↔ Courses |
- Relationship methods are defined directly on the model as regular PHP functions
- Calling a relationship as a property, not a method, retrieves the related data
- Eloquent infers foreign key names by convention, though they can be customized
These three cover most everyday cases, but some data connections span an extra table in between — which is exactly what "has one/many through" relationships are designed to handle.
Ready to master Laravel Training Course?
Join Uncodemy's hands-on Laravel program and build real, production-ready applications.
