Introduction
Eloquent makes it easy to manage relationships between database tables. Lyger supports several types of relationships including One-to-One, One-to-Many, and Many-to-Many, allowing you to work with related data in an expressive and intuitive way.Relationship Types
Lyger supports the following relationship types:- One-to-One:
hasOne()andbelongsTo() - One-to-Many:
hasMany()andbelongsTo() - Many-to-Many:
belongsToMany()
One-to-One Relationships
Defining the Relationship
A one-to-one relationship is used when one record is related to exactly one other record.app/Models/User.php
app/Models/Profile.php
Database Structure
Using the Relationship
One-to-Many Relationships
Defining the Relationship
A one-to-many relationship is used when one record can have multiple related records.app/Models/User.php
app/Models/Post.php
Database Structure
Using the Relationship
Many-to-Many Relationships
Defining the Relationship
A many-to-many relationship is used when records on both sides can have multiple related records.app/Models/Post.php
app/Models/Tag.php
Database Structure
The pivot table name is automatically determined by combining the related model names in alphabetical order:
post_tag. You can customize this by passing a second parameter to belongsToMany().Using the Relationship
Custom Foreign Keys
One-to-One and One-to-Many
By default, Eloquent assumes the foreign key is{model_name}_id. You can customize this:
Many-to-Many
Customize the pivot table name and keys:Relationship Methods
hasOne()
Defines a one-to-one relationship (parent side).The fully qualified class name of the related model
string
The foreign key on the related model (defaults to
{model}_id)hasMany()
Defines a one-to-many relationship (parent side).The fully qualified class name of the related model
string
The foreign key on the related model (defaults to
{model}_id)belongsTo()
Defines the inverse of a one-to-one or one-to-many relationship.The fully qualified class name of the related model
string
The foreign key on this model (defaults to
{related_model}_id)belongsToMany()
Defines a many-to-many relationship.The fully qualified class name of the related model
string
The name of the pivot table (defaults to alphabetically ordered model names)
Complete Example
Here’s a comprehensive example with multiple relationship types:app/Models/User.php
app/Models/Post.php
app/Models/Comment.php
app/Models/Tag.php
Using the Relationships
Collection Methods on Relationships
Relationship results returnCollection instances with useful methods:
Best Practices
1
Use Descriptive Method Names
Name relationship methods clearly based on what they return:
2
Define Inverse Relationships
Always define both sides of a relationship:
3
Use Type Hints
Document return types for better IDE support:
4
Follow Naming Conventions
Use standard naming for pivot tables and foreign keys:
- Pivot tables: alphabetically ordered model names (
post_tag) - Foreign keys:
{model}_id(e.g.,user_id)
Migration Examples
One-to-Many Migration
Many-to-Many Migration
Next Steps
Eloquent Models
Learn more about Eloquent model features
Query Builder
Understand the underlying Query Builder