Skip to content
Laureano F. Lamonega edited this page Jul 17, 2026 · 4 revisions

Model

The base model provides CRUD operations and automatic table name guessing.

Defining a Model

use Antimonial\Model\Model;

class User extends Model {}

Table Name Convention

PascalCase class names are converted to snake_case plural:

Class Table Name
User users
BlogPost blog_posts
Category categorys (naive — set $table explicitly for irregular plurals)

To use a custom table name, override the $table property:

class Category extends Model
{
    protected string $table = 'categories';
}

Primary Key

The default primary key is id. Override with:

class User extends Model
{
    protected string $primaryKey = 'user_id';
}

Timestamps

Enable automatic created_at / updated_at:

class User extends Model
{
    protected bool $timestamps = true;
}

CRUD Operations

All CRUD methods support both static and instance syntax:

Find

$user = User::find(42);          // static (preferred)
$user = (new User())->find(42);  // instance — also works

All

$users = User::all();

Where

$users = User::where('active', true)->orderBy('name')->get();

Insert

$id = User::insert([               // static
    'name'  => 'John',
    'email' => 'john@example.com',
]);

Update

User::update(42, ['name' => 'Jane']);

Delete

User::delete(42);

Query Builder Access

$qb = User::query();                                      // Get the underlying QueryBuilder
$user = User::query()->where('email', 'john@example.com')->first();

Clone this wiki locally