Introduction
Lyger provides a powerful database layer inspired by Laravel’s Eloquent ORM and Query Builder. It supports multiple database drivers including SQLite, PostgreSQL, MySQL, and MariaDB, making it easy to work with databases in your PHP applications.
Database Configuration
Database configuration is managed through environment variables in your .env file.
SQLite
PostgreSQL
MySQL
MariaDB
SQLite is the default database driver and requires no additional setup. DB_CONNECTION = sqlite
DB_DATABASE = database/database.sqlite
SQLite is perfect for development and small applications. The database file will be automatically created in the database/ directory.
Configure PostgreSQL connection: DB_CONNECTION = postgres
DB_HOST = 127.0.0.1
DB_PORT = 5432
DB_DATABASE = lyger
DB_USERNAME = postgres
DB_PASSWORD =
PostgreSQL support requires the Rust database drivers to be installed.
Configure MySQL connection: DB_CONNECTION = mysql
DB_HOST = 127.0.0.1
DB_PORT = 3306
DB_DATABASE = lyger
DB_USERNAME = root
DB_PASSWORD =
Configure MariaDB connection (same as MySQL): DB_CONNECTION = mariadb
DB_HOST = 127.0.0.1
DB_PORT = 3306
DB_DATABASE = lyger
DB_USERNAME = root
DB_PASSWORD =
Database Structure
Lyger automatically creates and manages your database directory structure:
your-project/
├── database/
│ ├── database.sqlite # SQLite database file
│ └── migrations/ # Migration files
└── .env # Environment configuration
Quick Example
Here’s a quick example to get you started with database operations:
Create a Model
Create a model class that extends Lyger\Database\Model: <? php
namespace App\Models ;
use Lyger\Database\ Model ;
class User extends Model
{
protected string $table = 'users' ;
protected array $fillable = [ 'name' , 'email' , 'password' ];
protected array $hidden = [ 'password' ];
}
Query the Database
Use the model to interact with your database: // Find a user by ID
$user = User :: find ( 1 );
// Create a new user
$user = User :: create ([
'name' => 'John Doe' ,
'email' => 'john@example.com' ,
'password' => password_hash ( 'secret' , PASSWORD_DEFAULT )
]);
// Get all users
$users = User :: all ();
// Query with conditions
$activeUsers = User :: query ()
-> where ( 'status' , '=' , 'active' )
-> orderBy ( 'created_at' , 'DESC' )
-> get ();
Update and Delete
Modify existing records: // Update a user
$user = User :: find ( 1 );
$user -> name = 'Jane Doe' ;
$user -> save ();
// Delete a user
$user = User :: find ( 1 );
$user -> delete ();
Database Connection
The database connection is automatically managed by Lyger. The framework handles:
Connection pooling
Error handling with exceptions
PDO attribute configuration
Automatic directory creation
Manual Connection Access
If you need direct PDO access, you can get it through the QueryBuilder:
use Lyger\Database\ QueryBuilder ;
$pdo = ( new QueryBuilder ( 'users' )) -> getConnection ();
Manual PDO access should be used sparingly. The Query Builder and Eloquent ORM provide safer, more convenient methods for most use cases.
Error Handling
Lyger uses PDO exceptions for database errors:
try {
$user = User :: findOrFail ( 999 );
} catch ( \ RuntimeException $e ) {
// Handle not found error
echo "User not found: " . $e -> getMessage ();
}
try {
User :: create ([ 'invalid' => 'data' ]);
} catch ( \ PDOException $e ) {
// Handle database errors
echo "Database error: " . $e -> getMessage ();
}
Next Steps
Query Builder Learn about the fluent Query Builder interface
Eloquent Models Explore the Eloquent ORM features
Migrations Manage your database schema with migrations
Relationships Define relationships between models