Documentation Index
Fetch the complete documentation index at: https://mintlify.com/charlietyn/rest-generic-class/llms.txt
Use this file to discover all available pages before exploring further.
Overview
TheBaseModel 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 extendBaseModel instead of Laravel’s default Model:
Required Constants
MODEL
Defines the model name used in API requests:RELATIONS
Declares which relations are allowed for eager loading and filtering. This is critical for security — only relations listed here can be loaded via therelations parameter.
HIERARCHY_FIELD_ID (Optional)
Enables hierarchical queries for self-referencing models:- Hierarchical listing with the
hierarchyparameter - Helper methods:
hierarchyParent(),hierarchyChildren() - Tree-building capabilities
Role-Based Field Access Control
ThefieldsByRole property enables fine-grained field-level access control using Spatie roles.
Basic Usage
How It Works
Fields NOT listed infieldsByRole are writable by any authenticated user (base fields).
Fields listed under a role are “privileged” and require that specific role:
is_superuser,permissions→ Onlysuperadminrole can writestatus,role_id→ Onlyadminrole can writename,email,password→ Any authenticated user can write (not restricted)
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
ThegetDeniedFieldsForUser() method returns fields the user cannot write:
- If
fieldsByRoleis empty →[](no restrictions) - If
user->is_superuser === true→[](unrestricted) - Otherwise:
- Universe = all fields mentioned in
fieldsByRole - Allowed = fields the user CAN write (from their roles)
- Denied = Universe − Allowed
- Universe = all fields mentioned in
This method is consumed by:
- FilterRequestByRole middleware → strips denied fields from request payload
- BaseRequest::mergeProhibitedRules() → adds
prohibitedvalidation rules
Scenario-Based Validation
Models support scenario-based validation rules:createfor new recordsupdatefor existing records
Hierarchy Helper Methods
WhenHIERARCHY_FIELD_ID is defined, BaseModel provides:
Returns
true if the model has HIERARCHY_FIELD_ID defined.Returns a
belongsTo relation to the parent record.Returns a
hasMany relation to child records.Returns
true if the record has no parent (root node).Returns all ancestors from parent to root.
Returns all descendants. Optional
$maxDepth limits recursion depth.MongoDB Relations
BaseModel includes MongoDB relationship helpers for cross-database relations:Complete Example
Next Steps
- Services — Learn how BaseService uses your model
- Controllers — Set up REST endpoints
- Hierarchical Data — Work with tree structures