Skip to main content

Overview

BaseModel extends Laravel’s Eloquent Model with advanced features including:
  • Validation: Built-in validation with scenario support (create/update)
  • Hierarchy: Self-referencing tree structures with ancestor/descendant traversal
  • Role-based field restrictions: Control field access per Spatie role
  • Parent model support: Single Table Inheritance (STI) pattern
  • MongoDB relations: Custom relation helpers for MongoDB connections
All models in your application should extend BaseModel to leverage these features.

Constants

MODEL

The name of the model used for array parameter wrapping. Example:

columns

Default columns for the model. Used for field selection in queries. Example:

RELATIONS

Defines allowed relations that can be eager loaded via API requests. Example:
Only relations listed here can be loaded via the relations query parameter. This is a security feature.

PARENT

Defines parent class information for Single Table Inheritance hierarchy. Example:

HIERARCHY_FIELD_ID

Field name for self-referencing hierarchy (e.g., parent_id, manager_id). Example:

Properties

fieldsByRole

Role-to-field restriction map. Declares which fields require specific Spatie roles to write.
array<string, list<string>>
default:"[]"
Maps role names to arrays of privileged field names
Example:
Behavior:
  • Fields NOT listed are writable by any authenticated user
  • Superusers bypass all restrictions
  • Multiple roles grant union of field access

scenario

Current operation scenario (create or update). Used for context-aware validation.

fieldKeyUpdate

Reference column for update operations. Falls back to primary key when null.

Key Methods

getDeniedFieldsForUser()

Returns fields the given user is NOT allowed to write.
mixed
required
The authenticated user model (must have Spatie’s hasRole() method)
list<string>
Array of field names the user cannot write
Algorithm:
  1. If $fieldsByRole is empty → return [] (no restrictions)
  2. If user has is_superuser = true → return []
  3. Build universe of all privileged fields
  4. Build allowed set from user’s roles
  5. Return universe − allowed
Example:

Accessor Methods

getPrimaryKey()

Returns the model’s primary key name.

getFieldKeyUpdate()

Returns the field used for updates (defaults to primary key).

getScenario()

Returns current validation scenario (create or update).

setScenario()

Sets the validation scenario.

Hierarchy Methods

hasHierarchy()

Checks if model uses Single Table Inheritance (has PARENT defined).

hasHierarchyField()

Checks if model supports self-referencing hierarchy.

getHierarchyFieldId()

Returns the hierarchy field name (e.g., parent_id).

hierarchyParent()

Defines BelongsTo relation to parent in hierarchy. Example:

hierarchyChildren()

Defines HasMany relation to children in hierarchy. Example:

isHierarchyRoot()

Checks if record is root node (has no parent).

getHierarchyAncestors()

Returns all ancestors from parent to root. Example:

getHierarchyDescendants()

Returns all descendants recursively.
int|null
default:"null"
Maximum depth to traverse (null = unlimited)
int
default:"0"
Current depth in recursion (internal use)
Example:

Validation Methods

rules()

Define validation rules per scenario. Override in your model. Example:

self_validate()

Validates current model attributes.
string
default:"create"
Validation scenario (create or update)
bool
default:"false"
If true, only validate fields present in attributes
bool
default:"true"
Include primary key in validation
array

validate_all()

Validates model and all parent models in hierarchy.
array
required
Data to validate
string
default:"create"
Validation scenario
array

save_model()

Validates and saves the model (and parent hierarchy if applicable).
array
default:"[]"
Attributes to save (uses current attributes if empty)
string
default:"create"
Operation scenario
array

Static Methods

create_model()

Creates one or more model instances. Example:

update_multiple()

Updates multiple records at once. Example:

destroy_model()

Deletes a model by ID.
array

MongoDB Relation Methods

These methods enable relationships between SQL and MongoDB databases.

belongsToMongo()

Defines a BelongsTo relationship with a MongoDB model. Example:

hasManyMongo()

Defines a HasMany relationship with a MongoDB model. Example:

hasOneMongo()

Defines a HasOne relationship with a MongoDB model.

Usage Example

BaseService

Service layer with query building and CRUD operations

RestController

Controller exposing RESTful API endpoints