Skip to main content

Overview

The BaseModel class is the foundation of your Laravel models when using Rest Generic Class. It extends Laravel’s Eloquent Model and adds:
  • Automatic REST integration with services and controllers
  • Role-based field restrictions via fieldsByRole
  • Hierarchical data support with self-referencing relationships
  • Relation declaration for security and filtering
  • Built-in validation with scenario-based rules

Extending BaseModel

All your models should extend BaseModel instead of Laravel’s default Model:

Required Constants

MODEL

Defines the model name used in API requests:
This allows batch operations like:

RELATIONS

Declares which relations are allowed for eager loading and filtering. This is critical for security — only relations listed here can be loaded via the relations parameter.
If const RELATIONS is not defined and strict_relations is enabled (default), requests with relations parameter will throw a 500 error. Always declare your relations explicitly.
Now clients can request:

HIERARCHY_FIELD_ID (Optional)

Enables hierarchical queries for self-referencing models:
This activates:
  • Hierarchical listing with the hierarchy parameter
  • Helper methods: hierarchyParent(), hierarchyChildren()
  • Tree-building capabilities
Example for categories with parent-child structure:
See Hierarchical Data for full details.

Role-Based Field Access Control

The fieldsByRole property enables fine-grained field-level access control using Spatie roles.

Basic Usage

How It Works

Fields NOT listed in fieldsByRole are writable by any authenticated user (base fields). Fields listed under a role are “privileged” and require that specific role:
  • is_superuser, permissions → Only superadmin role can write
  • status, role_id → Only admin role can write
  • name, email, password → Any authenticated user can write (not restricted)
array<string, list<string>>
Maps Spatie role names to field names. Users without the required role will have those fields stripped from incoming requests and marked as prohibited in validation.

Resolution Logic

The getDeniedFieldsForUser() method returns fields the user cannot write:
  1. If fieldsByRole is empty → [] (no restrictions)
  2. If user->is_superuser === true[] (unrestricted)
  3. Otherwise:
    • Universe = all fields mentioned in fieldsByRole
    • Allowed = fields the user CAN write (from their roles)
    • Denied = Universe − Allowed
From BaseModel.php:111-138:
This method is consumed by:
  • FilterRequestByRole middleware → strips denied fields from request payload
  • BaseRequest::mergeProhibitedRules() → adds prohibited validation rules

Scenario-Based Validation

Models support scenario-based validation rules:
Scenarios are automatically set by the service layer:
  • create for new records
  • update for existing records

Hierarchy Helper Methods

When HIERARCHY_FIELD_ID is defined, BaseModel provides:
bool
Returns true if the model has HIERARCHY_FIELD_ID defined.
BelongsTo|null
Returns a belongsTo relation to the parent record.
HasMany
Returns a hasMany relation to child records.
bool
Returns true if the record has no parent (root node).
Collection
Returns all ancestors from parent to root.
Collection
Returns all descendants. Optional $maxDepth limits recursion depth.

MongoDB Relations

BaseModel includes MongoDB relationship helpers for cross-database relations:

Complete Example

Next Steps