PHP code example of hiblaphp / query-builder

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

    

hiblaphp / query-builder example snippets




ibla\QueryBuilder\DB;
use function Hibla\await;

// 1. Setup your schema asynchronously
await(DB::rawExecute("CREATE TABLE IF NOT EXISTS users (id INTEGER PRIMARY KEY, name TEXT)"));

// 2. Insert data
await(DB::table('users')->insert([
    'name' => 'Alice'
]));

// 3. Fetch data fluently
$user = await(DB::table('users')->where('name', 'Alice')->first());

echo "Hello, " . $user->name; // Outputs: Hello, Alice

// 4. Gracefully close connection pools when your application shuts down
DB::close();



namespace App\Repositories;

use Hibla\Promise\Interfaces\PromiseInterface;
use Hibla\QueryBuilder\Interfaces\DatabaseConnectionInterface;
use function Hibla\await;

class UserRepository
{
    public function __construct(
        private readonly DatabaseConnectionInterface $db
    ) {}

    /** @return PromiseInterface<list<array<string, mixed>>> */
    public function getActiveUsers(): PromiseInterface
    {
        return $this->db->table('users')
            ->where('status', 'active')
            ->latest()
            ->get();
    }

    public function createUser(array $data): PromiseInterface
    {
        return $this->db->table('users')->insertGetId($data);
    }
}

$activeUsers = DB::table('users')->where('status', 'active');

// These two queries run independently and safely.
// The original $activeUsers instance is NEVER mutated!
$admins = await($activeUsers->where('role', 'admin')->get());
$guests = await($activeUsers->where('role', 'guest')->get());
bash
cp vendor/hiblaphp/query-builder/hibla-database.php hibla-database.php