Skip to content

In this project, I have explored Laravel Eloquent relations.One to One,One to Many, Many to Mant (Has One Through), (Has Many Through) with documentation.

Notifications You must be signed in to change notification settings

liaquatalidev/Laravel-Eloquent-Relation

Repository files navigation

Laravel Eloquent Relations Documentation

Introduction

Laravel's Eloquent ORM facilitates the definition and management of relationships between database tables. These relationships simplify data retrieval and manipulation, enhancing code readability and maintainability. There are three primary types of relationships supported by Eloquent:

  • One-to-One.
  • One-to-Many.
  • Many-to-Many.
  • Has-One-Through.
  • Has-Many-Through.

This documentation provides an overview of each relationship type, including definitions, use cases, and best practices.

One-to-One Relationship

Definition

In a one-to-one relationship, each record in one table is associated with exactly one record in another table.

Use Cases (Example: Cars and Registrations)

  • Each car has exactly one registration.
  • Each registration belongs to only one car.

Best Practices

  • Utilize when a strict one-to-one correspondence exists between entities.
  • Use when data normalization is required to reduce redundancy.

One-to-Many Relationship

Definition

A one-to-many relationship signifies that each record in one table can be associated with multiple records in another table.

Use Cases (Example: Authors and Books)

  • An author can have multiple books.
  • Each book is authored by a single author.

Best Practices

  • Apply when one entity can have multiple related instances.
  • Suitable for hierarchical data structures where a parent entity manages subordinate entities.

Many-to-Many Relationship

Definition

Many-to-many relationships indicate that multiple records in one table can be associated with multiple records in another table.

Use Cases (Example: Tags,Posts and Post_Tag)

  • Posts and Tags: Each post can have multiple tags, and each tag can be associated with multiple posts.

Best Practices

  • Utilize many-to-many relationships for scenarios where entities have a mutually inclusive relationship.
  • Implement efficient indexing and optimization for large datasets.
  • Leverage Eloquent's methods for attaching, detaching, and syncing related records effectively.

Tables of Many-to-Many Relation

  • posts Table
  • tags Table
  • post_tag Table (Pivot Table)

Laravel Eloquent hasOneThrough Relation

Introduction

The hasOneThrough relationship in Laravel's Eloquent ORM allows you to define a relationship that spans through another relationship. This documentation outlines the usage of the hasOneThrough relationship for the profiles, countries, and users tables.

Definition

In a hasOneThrough relationship, a model obtains a related model through another intermediate relationship. It allows you to access data from a distant relationship through intermediate ones.

Use Case

We want to establish a hasOneThrough relationship to access a user's country through their profile.

Tables

  • profiles Table
  • countries Table
  • users Table

With the hasOneThrough relationship, you can seamlessly access a user's country through their profile. Adjust the code as needed based on your application's requirements.

Laravel Eloquent hasManyThrough Relation

Introduction

The hasManyThrough relationship in Laravel's Eloquent ORM allows you to define a relationship that involves a direct association between two models through an intermediate one. This documentation outlines the usage of the hasManyThrough relationship for the grandparents, parents, and children tables.

Use Case

We want to establish a hasManyThrough relationship to retrieve all children associated with a grandparent directly.

Tables

  • grandparents Table
  • parents Table
  • children Table

With the hasManyThrough relationship, you can seamlessly access all children associated with a grandparent directly through the intermediate Parent model. Adjust the code as needed based on your application's requirements.

Question

What's the difference between hasOneThrough and hasManyThrough ?

The difference between hasOneThrough and hasManyThrough lies in the cardinality of the relationship they represent in a database schema:

1:hasOneThrough

In this type of relationship, a model has exactly one related record through another intermediate model. It implies a one-to-one relationship through the intermediate model.

2:hasManyThrough

In this type of relationship, a model has many related records through another intermediate model. It implies a one-to-many relationship through the intermediate model.

example

Let's illustrate this with an example:

Consider three models/entities: Country, State, and City.

A Country has many States. A State belongs to a single Country. A State has many Cities. A City belongs to a single State. With hasOneThrough and hasManyThrough:

Country hasOneThrough City: This might imply that a Country has a single "representative" City that serves as its capital, for example. So, each Country has exactly one City through its State. Country hasManyThrough City: This could represent that a Country has multiple major cities through its States. Each State has multiple Cities, and there are multiple States in a Country. In summary, the difference is in the number of related records that can be accessed through the intermediate model: one for hasOneThrough and many for hasManyThrough.

Database file Click here

"Shortly, I'll be implementing a polymorphic relation, ensuring comprehensive documentation accompanies the process."

"Follow me if you find this documentation valuable."

Follow me on other platforms

Medium. Linkedin.

Created by Liaquat Ali

About

In this project, I have explored Laravel Eloquent relations.One to One,One to Many, Many to Mant (Has One Through), (Has Many Through) with documentation.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published