Lapis Docs

Data and code

Data tables

Not everything on a site is a page. Products, events or team members are records with the same fields, and a data table holds them. A data table is a file in data/ that returns a Table, like a Tempest config. Lapis creates the database table from it, migrates it when the definition changes, gives your templates a query builder and gives editors a panel for the records.

Defining a table

Each column is an object with a label, and its type decides how it is stored, validated and edited:

<?php // data/products.php
use Lapis\Data\Columns\{Image, Integer, RichText, Select, Slug, Text, Toggle};
use Lapis\Data\Table;

return new Table(
    label: 'Products',
    columns: [
        'name' => new Text('Name', required: true),
        'slug' => new Slug(from: 'name'),             // unique; from the name when empty
        'price' => new Integer('Price', index: true),
        'category' => new Select('Category', ['yoga' => 'Yoga', 'massage' => 'Massage']),
        'image' => new Image('Image'),
        'description' => new RichText('Description'),
        'available' => new Toggle('Available', default: true, index: true),
    ],
    title: 'name',                                    // shown in the list
    sortable: true,                                   // order by hand
);

These column types are available in Lapis\Data\Columns: Text, Textarea, RichText, Slug, Integer, Decimal, Toggle, Date, DateTime, Select, Image, Url and Email. Columns accept common options such as required:, default:, index:, unique:, max: and help:. Every table also gets id, sort, created_at and updated_at automatically.

To start a new definition, let the console write the file:

./lapis make:table events

The schema

The database table (data_products here) is created by itself, and a new column in the definition adds a column to it. Lapis never drops anything by itself. To drop a column or a table, look at the differences first, then apply them:

./lapis data:status
./lapis data:migrate --force

Querying

In templates and page functions, $cms->data() returns a query builder for a table:

$products = $cms->data('products')
    ->where('available', true)
    ->where('price', '<', 200)
    ->orderBy('price')
    ->get();

It also has ->whereIn($column, $values), ->search($q), ->limit($n), ->offset($n), ->first(), ->count(), ->paginate(page: 2, perPage: 12), ->find($id) and ->findBy('slug', $slug). ->where() accepts =, !=, <, >, <=, >= and like, and null checks IS NULL or IS NOT NULL. To write from PHP, use ->create([...]), ->update($id, [...]), ->delete($id) and ->reorder($ids). Only the columns of the definition are accepted, so it is safe to pass user input to it. Every call returns a new query, so one can be the base of several:

$available = $cms->data('products')->where('available', true);
$cheap = $available->where('price', '<', 200)->get();   // $available is unchanged
$count = $available->count();

->paginate() gives Tempest's PaginatedData: the records in data, and currentPage, totalPages, totalItems, hasNext, nextPage, hasPrevious, previousPage and pageRange (the page numbers to link). A page past the last one gives the last one. Pass it to <x-pagination :pages="$products" /> for accessible previous, page number and next links (or $cms->paginate($items, perPage: 12) and $cms->paginatePages(under: 'blog', perPage: 10) for arrays and pages).

<?php // pages/products.php
use Lapis\Routing\Get;

$load = #[Get] fn ($cms) => ['products' => $cms->data('products')
    ->search($cms->query('q'))
    ->paginate(page: (int) $cms->query('page', 1), perPage: 12)];
?>
<article :foreach="$products->data as $product">{{ $product['name'] }}</article>
<x-pagination :pages="$products" />

To build a page for every record, use a loader.

Editing and keeping the data

Editors find the tables in the toolbar under Data. They can list, search and order the records, and edit them in a form made from the definition, with errors shown per field.

  • ./lapis backup includes the tables.
  • A change to a record empties the page cache, so pages show the new data at once.
  • In tests, $this->table('products')->create([...]) adds records.

Schemaless collections

With documents: true, a table keeps its records as JSON in a shared data_documents table. Adding, removing or renaming a field then needs no migration, and older records show a new field's default. The query builder, the editor panel and the validation work the same way.

Tempest models and migrations

When a table needs a class of its own, for relations, your own methods or complex queries, write a Tempest model and a migration in the site's app/ folder. The site's composer.json maps the App\ namespace to app/ (the starter site has it), and Tempest discovery finds the classes.

<?php // app/Models/Booking.php
namespace App\Models;

#[Table('bookings')]
final class Booking
{
    use IsDatabaseModel;

    public PrimaryKey $id;

    public function __construct(public string $name = '', public string $day = '', public int $people = 1) {}
}
<?php // app/Migrations/CreateBookingsTable.php
final class CreateBookingsTable implements MigratesUp, MigratesDown
{
    public string $name = '2026-11-24_create_bookings_table';

    public function up(): QueryStatement
    {
        return new CreateTableStatement('bookings')->primary()->text('name')->text('day')->integer('people', default: 1);
    }

    public function down(): QueryStatement { return new DropTableStatement('bookings'); }
}

In templates and page functions, use the model as usual: Booking::select()->orderBy('day')->all() or Booking::create(...$values).

New migrations run by themselves on boot, after Lapis's own (theirs are named 0000-00-01_lapis_…, so they sort first), but only up. ./lapis migrate:status lists them. To roll back, run ./lapis tempest migrate:down, which rolls back every migration that has a down. The tables live in the same database, so ./lapis backup includes them, but the editor's Data panel only shows the tables of data/*.php.