Skip to main content

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 is the default database driver and requires no additional setup.
.env
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.

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:
1

Create a Model

Create a model class that extends Lyger\Database\Model:
app/Models/User.php
<?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'];
}
2

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();
3

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