API Reference
API Reference
Section titled “API Reference”The AutoCrud trait provides a comprehensive set of public methods for interacting with your models programmatically.
Core Methods
Section titled “Core Methods”getFields()
Section titled “getFields()”Returns the field definitions for the model.
protected static function getFields(): arrayReturns: Array of field definitions
Example:
$fields = User::getFields();getIncludes()
Section titled “getIncludes()”Returns relationships to be loaded with the model.
public static function getIncludes(): arrayReturns: Array of relationship names
Automatically includes:
'records.user'- Relationships defined in
getFields() - Relationships from
$externalRelations
getEndpoint()
Section titled “getEndpoint()”Returns the API endpoint for the model.
public static function getEndpoint($model = null): stringParameters:
$model(optional): Model instance or class name
Returns: API endpoint string (e.g., /laravel-auto-crud/users)
getModelName()
Section titled “getModelName()”Returns the camelCase model name.
public static function getModelName(): stringReturns: Model name in camelCase with initial lowercase
Example:
// For UserProfile modelUserProfile::getModelName(); // returns "userProfile"Validation Methods
Section titled “Validation Methods”getCustomRules()
Section titled “getCustomRules()”Returns custom validation rules.
public static function getCustomRules(): arrayReturns: Array of custom validation rules
Default: Empty array []
getCustomForbiddenActions()
Section titled “getCustomForbiddenActions()”Returns custom forbidden actions.
public static function getCustomForbiddenActions(): arrayReturns: Array of forbidden actions
Default: Empty array []
getForbiddenActions()
Section titled “getForbiddenActions()”Returns all forbidden actions (including custom ones).
public static function getForbiddenActions(): arrayReturns: Array of forbidden actions by role
Relationship Methods
Section titled “Relationship Methods”getExternalRelations()
Section titled “getExternalRelations()”Returns enriched external relationships.
public static function getExternalRelations(): arrayReturns: Array of external relationship definitions with added endpoints
Form and Table Methods
Section titled “Form and Table Methods”getFormFields()
Section titled “getFormFields()”Returns fields that should appear in forms.
public static function getFormFields(): arrayReturns: Array of fields where form === true, plus auto-generated comboField entries
getTableFields()
Section titled “getTableFields()”Returns fields that should appear in tables.
public static function getTableFields(): arrayReturns: Array of fields where table === true
getTableHeaders()
Section titled “getTableHeaders()”Returns calculated table headers.
protected static function getTableHeaders(): arrayReturns: Array of table header definitions
getModel()
Section titled “getModel()”Returns the complete payload for frontend consumption.
public static function getModel($processedModels = []): arrayParameters:
$processedModels: Array to prevent circular references
Returns: Complete model payload including:
- Field definitions
- Validation rules
- Relationships
- Endpoints
- Table headers
- Form fields
Key Field Methods
Section titled “Key Field Methods”getTableKeyFields()
Section titled “getTableKeyFields()”Returns field definitions for table key placeholders.
public static function getTableKeyFields(): arrayReturns: Array of field definitions used in tableKey templates
getFormKeyFields()
Section titled “getFormKeyFields()”Returns field definitions for form key placeholders.
public static function getFormKeyFields(): arrayReturns: Array of field definitions used in formKey templates
Record Relationship
Section titled “Record Relationship”records()
Section titled “records()”Returns the morphMany relationship to Record model.
public function records()Returns: MorphMany relationship instance
Calendar Configuration
Section titled “Calendar Configuration”$calendarFields
Section titled “$calendarFields”Static property for calendar field configuration.
protected static $calendarFields = [ 'title' => '({user.name}) - {reservation_place} {return_place}', 'start' => 'start_date', 'end' => 'end_date', 'separateEvents' => true, 'startClass' => 'start-event-class', 'endClass' => 'end-event-class', 'class' => 'default-event-class',];Static Properties
Section titled “Static Properties”$includes
Section titled “$includes”Always-loaded relationships.
protected static $includes = ['user', 'category'];$externalRelations
Section titled “$externalRelations”BelongsToMany external relationships.
protected static $externalRelations = [ [ 'relation' => 'vehicles', 'name' => 'Vehicles', 'model' => Vehicle::class, 'pivotTable' => 'reservations_vehicles', 'foreignKey' => 'reservation_id', 'relatedKey' => 'vehicle_id', // ... more configuration ],];$forbiddenActions
Section titled “$forbiddenActions”Actions forbidden by role.
protected static $forbiddenActions = [ 'user' => ['destroyPermanent', 'restore', 'destroy'], 'admin' => ['destroyPermanent'],];Internal Methods
Section titled “Internal Methods”__call()
Section titled “__call()”Dynamically resolves relationship methods.
public function __call($method, $parameters)Handles:
- BelongsTo relationships from
getFields() - MorphTo relationships from
getFields() - BelongsToMany relationships from
$externalRelations
handleRelation()
Section titled “handleRelation()”Constructs Eloquent relationships from field definitions.
protected function handleRelation($field)handleExternalRelation()
Section titled “handleExternalRelation()”Constructs BelongsToMany relationships from external relation definitions.
protected function handleExternalRelation($relation)usesSoftDeletes()
Section titled “usesSoftDeletes()”Detects if a model class uses SoftDeletes trait.
protected static function usesSoftDeletes($modelClass): boolUsage Examples
Section titled “Usage Examples”Getting Complete Model Data
Section titled “Getting Complete Model Data”// Get complete model payload for frontend$modelData = User::getModel();
// Structure includes:// - fields: field definitions// - rules: validation rules// - relationships: relationship data// - endpoints: API endpoints// - tableHeaders: table configuration// - formFields: form configurationWorking with Relationships
Section titled “Working with Relationships”// Get user relationship dynamically$user = $model->user; // Calls handleRelation() internally
// Get external relationships$vehicles = $reservation->vehicles; // Calls handleExternalRelation()Form and Table Integration
Section titled “Form and Table Integration”// Get fields for form rendering$formFields = User::getFormFields();
// Get fields for table rendering$tableFields = User::getTableFields();
// Get table headers$headers = User::getTableHeaders();Validation Integration
Section titled “Validation Integration”// Get custom validation rules$customRules = User::getCustomRules();
// Get forbidden actions for current user role$forbiddenActions = User::getForbiddenActions();API Endpoint Generation
Section titled “API Endpoint Generation”// Get model endpoint$endpoint = User::getEndpoint(); // "/laravel-auto-crud/users"
// Get specific model endpoint$userEndpoint = User::getEndpoint($userInstance);Automatic Initialization
Section titled “Automatic Initialization”The initializeAutoCrud() method automatically:
- Fills
$fillablewith all field names fromgetFields() - Builds
$castsbased on field types - Adds password fields to
$hiddenarray
This happens automatically when the trait is used, so you don’t need to manually define these properties for fields covered by getFields().
Error Handling
Section titled “Error Handling”The trait includes built-in error handling for:
- Circular relationship references
- Missing relationship models
- Invalid field configurations
- Validation failures
Always wrap API calls in try-catch blocks when using in production:
try { $modelData = User::getModel();} catch (\Exception $e) { // Handle error appropriately Log::error('Failed to get model data', ['error' => $e->getMessage()]);}