Skip to main content

Overview

Rest Generic Class provides powerful relation loading capabilities that allow clients to:
  • Eager-load relationships with the relations parameter
  • Select specific fields from relations
  • Filter and sort related data
  • Nest relations multiple levels deep
  • Load pivot data for many-to-many relationships

Basic Relation Loading

Declaring Relations

First, declare allowed relations in your model:
Only relations listed in const RELATIONS can be loaded. This is a security feature to prevent unauthorized data access.

Loading Relations

Load relations via the relations query parameter:
Response includes nested data:

Field Selection

Select Specific Fields

Limit fields from relations:
This loads only id and name from category, and rating and comment from reviews.
The primary key is always included automatically, even if not specified.

Combine with Main Model Selection

Returns:

Nested Relations

Loading Multi-Level Relations

Load relations of relations using dot notation:
All nested relations must be declared in the RELATIONS constant:

Deep Nesting Example

Request:

Filter on Relation Fields

Use the _nested parameter to apply oper filters to relations:
This filters products where reviews have a rating >= 4.

WhereHas Conditions

Filter parent records based on related data:
Returns only products where the category name is “Electronics”.

Complex Nested Filters

Relation Types

One-to-Many (HasMany)

Usage:
See ManagesOneToMany Trait for CRUD operations on one-to-many relations.

Many-to-One (BelongsTo)

Usage:

Many-to-Many (BelongsToMany)

Usage:
Pivot data is automatically included:
See ManagesManyToMany Trait for advanced many-to-many operations.

Has-One-Through and Has-Many-Through

Usage:

Polymorphic Relations

Usage:

Advanced Scenarios

Conditional Relations

Load different relations based on user role:

Counting Relations

Use withCount() for relation counts:
Request:
Response:

Aggregate Functions

Load computed values:

Relation Management Traits

Rest Generic Class provides traits for managing related records:

ManagesOneToMany

Provides CRUD endpoints for HasMany relationships:
See ManagesOneToMany Trait for details.

ManagesManyToMany

Provides CRUD endpoints for BelongsToMany relationships:
See ManagesManyToMany Trait for details.

Configuration

Configure relation behavior in config/rest-generic-class.php:

Performance Optimization

Eager Loading vs Lazy Loading

Good - Eager load to avoid N+1 queries:
Bad - Without eager loading, each product triggers separate queries:

Select Only Needed Fields

This reduces data transfer and JSON serialization overhead.

Limit Relation Depth

Avoid deeply nested relations in production:

Error Handling

Common relation errors:

Security Best Practices

  1. Always declare relations: Never allow arbitrary relation loading
  2. Use field selection: Prevent exposure of sensitive fields
  3. Validate nested filters: Ensure _nested doesn’t expose restricted data
  4. Limit depth: Set reasonable max_depth in config
  5. Check permissions: Use middleware to restrict sensitive relations
Example permission check: