Advanced Eloquent Relations in Laravel: Key Insights from Chris Morrell at Laracon US 2025

August 9, 2025 (4mo ago)

Jump to FAQs

Chris Morrell’s session at Laracon US 2025, “Advanced Eloquent Relations,” is a must-watch for any Laravel developer aiming to push the boundaries of what Eloquent ORM can do. He demonstrates that Eloquent relations are, at their core, just PHP code—meaning you can extend, customize, and even invent new types of relationships to solve unique business problems or data structures that don’t fit the usual conventions.

Core Takeaways

1. Eloquent Relations Are Just PHP Code

2. Custom Relations: When and Why

3. How Custom Relations Work

4. Practical Guidance

5. Real-World Examples

How This Applies to My Work


References:

Discuss this post:

Frequently Asked Questions

What are custom Eloquent relations in Laravel?

Custom Eloquent relations are user-defined relation classes that encapsulate non-standard data access patterns so you can keep using Eloquent's fluent, chainable API.

When should I build a custom Eloquent relation instead of using hasMany or belongsTo?

Create one only when legacy schemas, computed/virtual data, geospatial logic, or unconventional keying cannot be expressed cleanly with built‑in relations. Default to core relations first.

How do I implement a custom relation class in Laravel?

Extend Relation (or an existing relation), implement addEagerConstraints and match, and return a Relation instance from a model method so it plugs into eager loading, constraints, and serialization.

Can I eager load a custom relation?

Yes. If addEagerConstraints, initRelation, and match are implemented correctly, you can call with('myRelation') just like native relations, keeping N+1 queries under control.

How do I handle legacy comma-separated ID lists with Eloquent?

Parse the stored string into IDs inside a custom relation's constraints, run a whereIn query, then map results back in match—avoiding schema rewrites while keeping expressive model APIs.

How can I model geospatial or distance-based relations?

Write a relation that injects raw distance calculations (e.g. Haversine) or uses a spatial index extension, filtering by radius and ordering by computed distance in addEagerConstraints.

Can a relation return computed, fake, or AI-generated data?

Yes. A relation can hydrate arrays or DTOs into model-like objects without hitting the database, enabling demos, prediction features, or synthetic fixtures.

What performance considerations apply to custom relations?

Batch work in addEagerConstraints, avoid per-parent queries, cache expensive derived datasets, and prefer database-side filtering over in-PHP loops to prevent hidden N+1 issues.

How do I test a custom Eloquent relation?

Write unit tests asserting the relation returns expected collections, respects eager loading counts, and does not increase query count when loading multiple parents. Use assertDatabaseQueryCount helpers if available.

What are common pitfalls with custom relations?

Missing match logic, per-model queries, leaky abstractions mixing business logic, and unclear naming. Always document intent and verify eager loading reduces queries.

Do custom relations serialize and work with API resources?

Yes—once matched onto the model they behave like native relations for toArray(), JSON resources, and lazy/eager loading, provided you return Collections or model instances.

How should teams document and maintain custom relations?

Centralize them in a dedicated namespace, add PHPDoc usage examples, explain data shape & constraints, and link to architectural decisions to reduce onboarding friction.