Documentation · Chapter 13

Work with relations, migrations, and seed data.

Relations describe how records connect. Migrations evolve the schema, seeders create repeatable starting data, and diagnostics explain the capabilities of each server.

Every lesson starts from the problem, explains the mechanism, then gives a concrete verification.

Load relations without N+1 queries

HasMany, HasOne, BelongsTo, and BelongsToMany describe connections. with() groups eager loading to avoid issuing one additional query for every parent record.

phpaml — zsh
$posts = $context->set(Post::class)
    ->with('author', 'comments')
    ->all();

aml data:migrate --connection main
aml data:seed --connection main

Evolve schemas with migrations

Schema and Table express portable changes. Migration locks prevent two processes from applying the same version concurrently, and failures identify both the migration and direction.

Create repeatable data and useful diagnostics

Seeders run transactionally and should create deterministic development or reference data. data:doctor reports drivers, connection, server version, transactions, savepoints, returning, and foreign-key support without printing passwords.