Skip to main content

Overview

The Model class provides an Eloquent-style ORM interface for interacting with database tables. It supports relationships, query building, mass assignment protection, attribute casting, and timestamps.

Class Reference

Lyger\Database\Model

Base class for all Eloquent models, inspired by Laravel Eloquent. Location: Lyger/Database/Model.php

Properties

string
The database table associated with the model. If not set, defaults to the pluralized, snake_case class name.
string
default:"'id'"
The primary key for the model.
array
default:"[]"
The attributes that are mass assignable.
array
default:"[]"
The attributes that should be hidden for serialization.
array
default:"[]"
The attributes that should be cast to native types.
array
default:"['created_at', 'updated_at', 'deleted_at']"
The attributes that should be mutated to dates.
bool
default:"true"
Indicates if the model should be timestamped.

Methods

Constructor

Create a new model instance with optional attributes.
array
default:"[]"
Initial attributes for the model
Example:

Query Methods

query()

Get a new query builder instance for the model. Returns: QueryBuilder instance Example:

find()

Find a model by its primary key.
mixed
The primary key value
Returns: Model instance or null if not found Example:

findOrFail()

Find a model by its primary key or throw an exception.
mixed
The primary key value
Returns: Model instance Throws: \RuntimeException if model not found Example:

all()

Get all records from the database. Returns: Collection of model instances Example:

create()

Create and save a new model instance.
array
The attributes for the new model
Returns: Created model instance Example:

Instance Methods

fill()

Fill the model with an array of attributes. Only fills $fillable attributes.
array
Attributes to fill
Returns: $this for method chaining Example:

save()

Save the model to the database. Performs an update if the model exists, otherwise inserts a new record. Returns: true on success, false on failure Example:

delete()

Delete the model from the database. Returns: true if deleted, false otherwise Example:

toArray()

Convert the model to an array, respecting $hidden attributes and applying casts. Returns: Array representation of the model Example:

toJson()

Convert the model to JSON.
int
default:"0"
JSON encoding options
Returns: JSON string Example:

getKey()

Get the primary key value for the model. Returns: Primary key value or null Example:

Relationship Methods

hasOne()

Define a one-to-one relationship.
The related model class name
string
The foreign key name (defaults to {model}_id)
Returns: HasOne relationship instance Example:

hasMany()

Define a one-to-many relationship.
The related model class name
string
The foreign key name (defaults to {model}_id)
Returns: HasMany relationship instance Example:

belongsTo()

Define an inverse one-to-one or one-to-many relationship.
The related model class name
string
The foreign key name
Returns: BelongsTo relationship instance Example:

belongsToMany()

Define a many-to-many relationship.
The related model class name
string
The pivot table name (defaults to alphabetically sorted model names)
Returns: BelongsToMany relationship instance Example:

Usage Examples

Basic Model Definition

Attribute Casting

Magic Methods

The Model class supports magic property access:

Timestamps

Models automatically manage created_at and updated_at timestamps:
To disable timestamps, set protected bool $timestamps = false in your model.

Best Practices

  • Always define $fillable to protect against mass assignment vulnerabilities
  • Use $hidden to exclude sensitive attributes from JSON/array conversion
  • Define relationships in dedicated methods for better organization
  • Use attribute casting for type safety