PHP code example of alphavel / database

1. Go to this page and download the library: Download alphavel/database 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/ */

    

alphavel / database example snippets


use Alphavel\Database\DB;

// 1. Single record lookup (17,000+ req/s)
$user = DB::findOne('users', 123);
// SELECT * FROM users WHERE id = ?
// Statement cached globally, zero overhead!

// 2. Batch queries (8,700+ req/s - 625% faster!)
$users = DB::findMany('users', [1, 2, 3, 4, 5]);
// SELECT * FROM users WHERE id IN (1,2,3,4,5)
// Single query instead of 5 queries!

// 3. Multiple different records (10,500+ req/s)
[$user, $product, $order] = DB::batchFetch('entities', [$userId, $productId, $orderId]);
// Reuses same cached statement 3 times

// Use when you need filters, joins, pagination
$users = DB::table('users')
    ->where('status', 'active')
    ->where('age', '>', 18)
    ->orderBy('created_at', 'DESC')
    ->limit(10)
    ->get();

// Insert
DB::table('users')->insert([
    'name' => 'John Doe',
    'email' => '[email protected]',
]);

// Transactions
DB::transaction(function() {
    DB::execute('INSERT INTO orders ...');
    DB::execute('INSERT INTO order_items ...');
});

use App\Models\User;

// Find
$user = User::find(1);
$users = User::where('active', true)->get();

// Create
$user = User::create([
    'name' => 'Jane Doe',
    'email' => '[email protected]',
]);

// Update
$user->name = 'John Smith';
$user->save();

// Relationships
$posts = $user->posts;  // Lazy loading
$users = User::with('posts')->get();  // Eager loading