data-table provides relation-first query APIs with support for hasMany, hasOne, belongsTo, and hasManyThrough relationships.
Defining Relations
hasMany
Defines a one-to-many relationship where the foreign key lives on the target table:hasMany infers the foreign key as user_id (singular source table name + _id).
Custom Keys
hasOne
Defines a one-to-one relationship where the foreign key lives on the target table:belongsTo
Defines a one-to-one relationship where the foreign key lives on the source table:belongsTo infers the foreign key as user_id and the target key as the primary key of the target table.
Custom Keys
hasManyThrough
Defines a many-to-many relationship through an intermediate table:Eager Loading Relations
Load relations with the.with() method:
Query Builder
CRUD Helpers
Relation Modifiers
Filter, sort, and limit related records:Available Modifiers
Nested Relations
Eager load relations of relations:Relation Types
Cardinality
hasManyandhasManyThroughreturn arrays:Target[]hasOneandbelongsToreturn nullable objects:Target | null
Relation Queries
Relations are composable with all query builder methods:Composite Keys
Relations support composite keys:Type Inference
Performance Tips
- Use relation modifiers to limit loaded data:
- Avoid N+1 queries by eager loading relations instead of loading them in loops:
- Use joins for filtering, eager loading for data: