PHP code example of webmavens / laravel-remote-http-database

1. Go to this page and download the library: Download webmavens/laravel-remote-http-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/ */

    

webmavens / laravel-remote-http-database example snippets


'connections' => [
    // ... other connections
    
    'remote-http' => [
        'driver' => 'remote-http',
        'endpoint' => env('DB_REMOTE_ENDPOINT'),
        'api_key' => env('DB_REMOTE_API_KEY'),
        'encryption_key' => (function() {
            $key = env('DB_REMOTE_ENCRYPTION_KEY');
            // Return null if key is not set (prevents deprecation warning on Server 1)
            if ($key === null) {
                return null;
            }
            // Try to decode base64, fallback to raw key if decoding fails or length is wrong
            $decoded = base64_decode($key, true);
            return ($decoded !== false && strlen($decoded) === 32) ? $decoded : $key;
        })(),
        'database' => env('DB_DATABASE', 'laravel'),
        'timeout' => env('DB_REMOTE_TIMEOUT', 30),
        'verify_ssl' => env('DB_REMOTE_VERIFY_SSL', true),
        'retry_attempts' => env('DB_REMOTE_RETRY_ATTEMPTS', 3),
        'enable_batching' => env('DB_REMOTE_ENABLE_BATCHING', true),
        'enable_caching' => env('DB_REMOTE_ENABLE_CACHING', true),
        'cache_ttl' => env('DB_REMOTE_CACHE_TTL', 60),
        'prefix' => '',
        'prefix_indexes' => true,
    ],
],

DB::connection()->select('SELECT 1 as test');


 = ke(\Illuminate\Contracts\Console\Kernel::class)->bootstrap();

try {
    $result = DB::connection()->select('SELECT 1 as test');
    echo "✓ Connection successful!\n";
} catch (\Exception $e) {
    echo "✗ Connection failed: " . $e->getMessage() . "\n";
}

// Eloquent models work normally
$user = User::find(1);

// Query builder works normally
$users = DB::table('users')->where('active', 1)->get();

// Transactions work normally
DB::transaction(function () {
    User::create(['name' => 'John']);
    Post::create(['title' => 'Hello']);
});

// Migrations work normally
php artisan migrate

   // Instead of N+1 queries
   $users = User::all();
   foreach ($users as $user) {
       $user->posts; // N queries
   }
   
   // Use eager loading
   $users = User::with('posts')->get(); // 2 queries total
   
bash
   php -r "echo 'REMOTE_DB_API_KEY=' . bin2hex(random_bytes(32)) . PHP_EOL;"
   php -r "echo 'REMOTE_DB_ENCRYPTION_KEY=' . base64_encode(random_bytes(32)) . PHP_EOL;"
   
bash
# Generate a 32-byte encryption key (base64 encoded)
php -r "echo base64_encode(random_bytes(32));"

# Generate a secure API key (hex encoded, 64 characters)
php -r "echo bin2hex(random_bytes(32));"
bash
php artisan tinker
bash
php artisan config:clear
bash
   php artisan config:clear
   
bash
   php -r "echo base64_encode(random_bytes(32));"
   
bash
   php -r "echo bin2hex(random_bytes(16));"
   
bash
   php artisan config:clear