Relationships
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
One-to-One:
A single record in one table corresponds to exactly one record in another table.
Example: A
userstable and aprofiletable, where each user has one profile.
One-to-Many:
A single record in one table can be associated with multiple records in another table.
Example: An
authorstable and abookstable, where one author can write many books.
Many-to-Many:
Multiple records in one table can be associated with multiple records in another table.
Example: A
studentstable and acoursestable, where one student can take many courses, and one course can be taken by many students.
Example:
For example, we have 3 tables
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
The include method is flexible as well to the where, order, group , limit, offset etc.
Last updated