network-wiredRelationships

Relationships are the connections between different tables in a database. These relationships allow you to efficiently store and retrieve related data. PHP is often used to interact with MySQL databases and manage these relationships.

Common Types of Relationships

  1. One-to-One:

    • A single record in one table corresponds to exactly one record in another table.

    • Example: A users table and a profile table, where each user has one profile.

  2. One-to-Many:

    • A single record in one table can be associated with multiple records in another table.

    • Example: An authors table and a books table, where one author can write many books.

  3. Many-to-Many:

    • Multiple records in one table can be associated with multiple records in another table.

    • Example: A students table and a courses table, where one student can take many courses, and one course can be taken by many students.

Example:

For example, we have 3 tables

Table
Fields

news

id, name, status

news_details

id, news_id, content

news_attachments

id, news_details_id, url

Each table is referenced to another table where news -> news_details -> news_attachments.

Usage:

However, if we transform the options into

What if the requirement is to return only a single record? Just pass single attribute to the object

circle-info

The include option can also be added inside the relation to create a hierarchy of data

The include method is flexible as well to the where, order, group , limit, offset etc.

Last updated