1. Go to this page and download the library: Download craftix/lite-eloquent library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
craftix / lite-eloquent example snippets
use Craftix\Eloquent\DB;
use PDO;
// Create a PDO connection
$pdo = new PDO(
'mysql:host=localhost;dbname=db_name',
'username',
'password',
[PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION]
);
// Initialize DB instance
$db = new DB($pdo);
// Start querying!
$users = $db->table('users')->get();
use Craftix\Eloquent\DatabaseManager;
use PDO;
// Initialize singleton
DatabaseManager::init(
new PDO('mysql:host=localhost;dbname=db_name', 'username', 'password')
);
// Use static methods
$users = DatabaseManager::table('users')->get();
// Get all users
$users = $db->table('users')->get();
// Select specific columns
$users = $db->table('users')
->select('id', 'name', 'email')
->get();
// Get first record
$user = $db->table('users')->first();
// Get single column as array
$emails = $db->table('users')->pluck('email');
// Simple where
$users = $db->table('users')
->where('status', '=', 'active')
->get();
// Where with default operator (=)
$users = $db->table('users')
->where('age', 25)
->get();
// Multiple conditions (AND)
$users = $db->table('users')
->where('status', 'active')
->where('age', '>', 18)
->get();
// OR conditions
$users = $db->table('users')
->where('status', 'active')
->orWhere('status', 'pending')
->get();
// WHERE IN
$users = $db->table('users')
->whereIn('id', [1, 2, 3, 4, 5])
->get();
// WHERE NOT IN
$users = $db->table('users')
->whereNotIn('status', ['banned', 'deleted'])
->get();
// WHERE BETWEEN
$users = $db->table('users')
->whereBetween('age', 18, 65)
->get();
// WHERE NULL / NOT NULL
$users = $db->table('users')
->whereNull('deleted_at')
->get();
$users = $db->table('users')
->whereNotNull('email_verified_at')
->get();
// WHERE LIKE
$users = $db->table('users')
->whereLike('name', '%john%')
->get();
// WHERE NOT LIKE
$users = $db->table('users')
->whereNotLike('email', '%@spam.com')
->get();
// Nested WHERE groups
$users = $db->table('users')
->where('status', 'active')
->where(function($query) {
$query->where('age', '>', 18)
->orWhere('verified', true);
})
->get();
use Craftix\Eloquent\Grammar\Expression;
// Using raw expressions in queries
$users = $db->table('users')
->select($db->raw('COUNT(*) as total'))
->get();
// Raw expressions in WHERE
$users = $db->table('users')
->where($db->raw('DATE(created_at)'), '=', '2024-01-01')
->get();
// Raw expressions in JOIN
$users = $db->table('users')
->join('profiles', $db->raw('users.id'), '=', $db->raw('profiles.user_id'))
->get();
// Or use Expression::make()
$expr = Expression::make('NOW()');
$users = $db->table('users')
->where('created_at', '>', $expr)
->get();
// Using DB instance
$db->beginTransaction();
try {
$db->table('users')->insert(['name' => 'John']);
$db->table('profiles')->insert(['user_id' => 1, 'bio' => 'Bio']);
$db->commit();
} catch (Exception $e) {
$db->rollBack();
throw $e;
}
// Using transaction closure
$db->transaction(function() use ($db) {
$db->table('users')->insert(['name' => 'John']);
$db->table('profiles')->insert(['user_id' => 1, 'bio' => 'Bio']);
});
// Check if in transaction
if ($db->inTransaction()) {
// Currently in a transaction
}
// Using singleton
DatabaseManager::beginTransaction();
try {
DatabaseManager::table('users')->insert(['name' => 'John']);
DatabaseManager::commit();
} catch (Exception $e) {
DatabaseManager::rollBack();
}
// Or with closure
DatabaseManager::transaction(function() {
DatabaseManager::table('users')->insert(['name' => 'John']);
});
$users = $db->table('users')->get();
// Collection methods
$users->count(); // Get count
$users->isEmpty(); // Check if empty
$users->first(); // Get first item
$users->last(); // Get last item
$users->pluck('email'); // Get array of email values
$users->plucked('email'); // Get new collection with emails
// Filtering and mapping
$users->filter(fn($user) => $user['age'] > 18);
$users->map(fn($user) => strtoupper($user['name']));
// Searching
$user = $users->find(fn($user) => $user['id'] === 1);
$exists = $users->contains(['id' => 1]);
// Array access
$firstUser = $users[0];
$users[] = ['id' => 100, 'name' => 'New User'];
// Convert to array/JSON
$array = $users->toArray();
$json = $users->toJson();
// Convert results to custom objects
$users = $db->table('users')
->objectConverter(function($row) {
return new User($row);
})
->get();
// Now $users contains User objects instead of arrays
foreach ($users as $user) {
echo $user->getName();
}
// Get the SQL query without executing
$sql = $db->table('users')
->where('status', 'active')
->toSql();
echo $sql; // SELECT * FROM `users` WHERE `status` = :v1;
use Craftix\Eloquent\Grammar\Dialects\MySqlDialect;
use Craftix\Eloquent\Grammar\Dialects\PostgresDialect;
// Set MySQL dialect
$db->setDialect(new MySqlDialect());
// Set Pgsql dialect
$db->setDialect(new PostgresDialect());
// ✅ Safe - Uses parameter binding
$users = $db->table('users')
->where('email', $userEmail)
->get();
// ⚠️ Only use raw expressions when absolutely necessary
$users = $db->table('users')
->where($db->raw('email'), '=', $userEmail) // Still safe due to binding
->get();