Overview
TheModel class provides an elegant ActiveRecord implementation for ServiceSQL. Models represent tables in your spreadsheet and provide intuitive methods for querying, creating, updating, and deleting records.
Setup
Creating a Model
Model Configuration
Static Properties
Static Properties
Table/sheet name (defaults to class name)
Primary key column name
Enable automatic
created_at and updated_at managementArray of columns that can be mass-assigned.
null means all columns are fillableDefine type casting for attributesSupported types:
int, integer, float, double, string, bool, boolean, json, array, date, datetime, dmy, dmyhmsColumns that should be cast to Date objects
Enable soft deletes (sets
deleted_at instead of removing records)Column name for soft delete timestamp
Static Methods
Initialization
use()
use()
Manually set the database connection for a model.
The database connection instance
This is automatically called when using
db.model(). Only use this for manual configuration.boot()
boot()
Lifecycle hook called once when the model is initialized. Override this to set up observers, global scopes, etc.
Query Methods
query()
query()
Get a new query builder instance for the model.
Returns a QueryBuilder instance with the model’s table and soft delete handling configured
where()
where()
all()
all()
Retrieve all records from the model’s table.
Returns a Collection of model instances
first()
first()
find()
find()
findOrFail()
findOrFail()
create()
create()
Eager Loading
with()
with()
Scopes
scope()
scope()
Soft Deletes
withTrashed()
withTrashed()
Include soft-deleted records in queries.
Returns the model class with trash mode set to ‘with’
onlyTrashed()
onlyTrashed()
Retrieve only soft-deleted records.
Returns the model class with trash mode set to ‘only’
Observers
observe()
observe()
Register an observer for model events.
Object with event handler methods or a function that receives (eventName, instance)
Available events:
creating, created, updating, updated, deleting, deletedInstance Methods
save()
save()
Save the model instance (create or update).
Returns the model instance
Automatically determines whether to create or update based on primary key presence.
update()
update()
delete()
delete()
Delete the model instance.
Returns true if deleted successfully
If soft deletes are enabled, sets
deleted_at timestamp instead of removing the record.fill()
fill()
toJSON()
toJSON()
Convert the model to a plain JavaScript object.
Returns a plain object with all non-private properties
Relationships
belongsTo()
belongsTo()
hasMany()
hasMany()
hasOne()
hasOne()
manyToMany()
manyToMany()
Define a many-to-many relationship.
The related model class
Name of the pivot/junction table
Foreign key for this model in pivot table (defaults to
{this._table}_id)Foreign key for related model in pivot table (defaults to
{RelatedClass._table}_id)Returns a relationship definition object
Accessors & Mutators
Accessors (Getters)
Accessors (Getters)
Define custom attribute accessors to transform data when retrieving.
Accessor methods follow the pattern:
get{AttributeName}Attribute(value)Mutators (Setters)
Mutators (Setters)
Define custom attribute mutators to transform data before saving.
Mutator methods follow the pattern:
set{AttributeName}Attribute(value) and must be static