Databases in Leaf
Leaf gives you three ways to work with your data. Db is a query builder for any PHP app. Eloquent models come with Leaf MVC. Schema files describe a table, its seeds and its fake data in one YAML file.
| id | name | role |
|---|
Toggle a chip, and the query, the generated SQL and the results stay in sync. Values always travel as bindings.
Pick your tool
In Leaf MVC they read the same database settings from your .env, so you can mix them in one app.
leafs/db
A query builder for any PHP app, with or without Leaf. You write queries without setting up model classes. Use it for scripts and small APIs.
composer require leafs/db
Leaf MVC
Laravel's Eloquent ORM, set up for you in Leaf MVC. Use it when you want a class per table, with relationships and casts.
leaf create my-app --mvc
leafs/schema
One YAML file per table holds its columns and seed data. Leaf MVC turns it into migrations and seeders for you.
leaf g:schema users
Db · leafs/db
Db gives MySQL, Postgres, SQLite and SQL Server one small API. It turns readable method chains into parameterised SQL.
Read the database docs.
$admins = db() ->select('users') ->where('role', 'admin') ->orderBy('created_at', 'desc') ->limit(10) ->all(); // or reach for the helpers $user = db()->select('users')->find(1); $vips = db()->select('users') ->where('plan', 'in', ['pro', 'team'])->all();
Db in practice
Values travel as bindings, and a unique check comes back as a plain error array.
$saved = db()
->insert('users')
->params([
'name' => $name,
'email' => $email,
])
->unique('email')
->execute();
if (!$saved) {
// ['email' => 'email already exists']
}
Load a record with its related rows in one call.
// an author, with all their posts
$author = db()->select('users')
->where('id', 1)
->with('posts')
->first();
Name your connections and switch per query with use().
db()->connect([ 'main' => [/* mysql */], 'analytics' => [/* postgres */], ], 'main');
Eloquent · Leaf MVC
Leaf MVC ships with Eloquent, the ORM from Laravel, and connects it from your .env. Each model is a class tied to one table.
Read the models docs.
class Flight extends Model { public function passengers() { return $this->hasMany(Passenger::class); } } // anywhere in your app $flights = Flight::where('active', 1) ->orderBy('name', 'desc') ->take(10) ->get(); $flights[0]->passengers()->count();
Eloquent in practice
Describe how models connect once, then follow the link from any record.
public function flight()
{
return $this->belongsTo(Flight::class);
}
// anywhere in your app
$passenger->flight->name;
Keep formatting rules on the model so every page gets the same value.
protected $casts = [
'active' => 'boolean',
];
public function getNameAttribute($value)
{
return ucfirst($value);
}
Remove a row from view without losing it.
$flight->delete(); // still there when you need it Flight::withTrashed()->get();
Schema files · leafs/schema
A schema file describes a table, its seeds and its fake data. Leaf MVC turns it into migrations and seeders for you.
Read the schema files docs.
# ids and timestamps are added for you columns: name: string email: type: string unique: true age: integer seeds: count: 10 data: name: '@faker.name' email: '@faker.unique.safeEmail' age: '@faker.numberBetween(18, 65)'
Schema files in practice
List the columns. Ids and timestamps are added for you, and a column can be marked unique.
Run one command and Leaf MVC turns the file into a migration and a seeder.
leaf g:schema users
Faker rules in the same file give you realistic rows for development.
seeds:
count: 10
data:
name: '@faker.name'
email: '@faker.unique.safeEmail'
Just the query builder, in any PHP app:
A Leaf MVC app with everything above set up:
Then read the database docs.