Database
The project wires the SQL database layer through DatabaseServiceProvider, config/database.php, model generation, and migration commands.
Use this guide for connection setup, raw SQL, and transactions. For model querying, migrations, and document models, see:
Supported SQL engines in the framework today:
- MySQL
- PostgreSQL
- SQLite
- SQL Server
The app skeleton starts with MySQL, but the underlying SQL layer is not MySQL-only.
Configuration
Primary config lives in:
config/database.php.env
The default connection is:
'default' => env('DB_CONNECTION', 'mysql')
Current local Docker defaults:
DB_CONNECTION=mysql
DB_HOST=db
DB_PORT=3306
DB_DATABASE=myxa
DB_USERNAME=myxa
DB_PASSWORD=secret
You can add PostgreSQL, SQLite, or SQL Server connections in config/database.php when an app needs more than the default MySQL connection.
Raw Queries
Use the DB facade when you want direct SQL:
use Myxa\Support\Facades\DB;
$users = DB::select(
'SELECT id, email FROM users WHERE status = ? ORDER BY id',
['active'],
);
Insert:
$userId = DB::insert(
'INSERT INTO users (email, status) VALUES (?, ?)',
['john@example.com', 'active'],
);
Update:
DB::update(
'UPDATE users SET status = ? WHERE id = ?',
['inactive', $userId],
);
Delete:
DB::delete(
'DELETE FROM users WHERE id = ?',
[$userId],
);
Streaming Raw Results
Use cursor() when a raw query may return many rows and you do not want to load the full result set into memory:
use Myxa\Support\Facades\DB;
foreach (DB::cursor(
'SELECT id, email FROM users WHERE status = ? ORDER BY id',
['active'],
) as $row) {
// $row is an associative array.
}
For model-backed streaming and batching, see Large Result Sets, Cursors, and Batching.
Transactions
Use transaction() when a unit of work should commit or roll back together:
use Myxa\Support\Facades\DB;
DB::transaction(function (): void {
$userId = DB::insert(
'INSERT INTO users (email, status) VALUES (?, ?)',
['john@example.com', 'active'],
);
DB::insert(
'INSERT INTO profiles (user_id, display_name) VALUES (?, ?)',
[$userId, 'John'],
);
});
Manual transactions are also available:
DB::beginTransaction();
try {
DB::update(
'UPDATE users SET status = ? WHERE id = ?',
['inactive', 1],
);
DB::commit();
} catch (\Throwable $exception) {
DB::rollBack();
throw $exception;
}
When To Use What
- Use
DB::select(),insert(),update(), anddelete()for direct SQL. - Use
DB::transaction()around multi-step writes. - Use
DB::cursor()for large raw SQL reads. - Use Database Models and Queries for normal app data access.
- Use Database Migrations to evolve schema.
Further Reading
- Database Models and Queries
- Database Migrations
- Mongo Models
- Configuration
vendor/200mph/myxa-framework/src/Database/README.mdvendor/200mph/myxa-framework/src/Database/Query/README.md