smi2/phpclickhouse
PHP client for ClickHouse with an easy, fluent API. Supports queries and inserts, result sets, bindings, and connection configuration for fast analytics workflows. Suitable for Laravel and standalone PHP apps needing reliable ClickHouse access.
ClickHouse::table()).selectWithNativeParams()).DateTime64, UUID) or repository pattern for complex queries.| Risk Area | Assessment | Mitigation Strategy |
|---|---|---|
| Type Mismatch | Laravel’s Eloquent expects Carbon, but ClickHouse uses DateTime64. |
Use accessors/mutators or custom casts in Eloquent models. |
| Connection Pooling | No built-in Laravel connection pooling for ClickHouse. | Use PHP-CLI workers (for queues) or Redis-based connection pooling. |
| Async Query Handling | Laravel’s sync framework may block on async ClickHouse queries. | Use Laravel Queues (bus:work) for async operations or ReactPHP for async. |
| Schema Migrations | ClickHouse DDL differs from Laravel Migrations. | Use custom Artisan commands or FlySystem-based schema management. |
| Error Handling | ClickHouse errors differ from Laravel’s QueryException. |
Wrap in custom exceptions or use try-catch with ClickHouseDB\Exception. |
| Performance Overhead | HTTP-based client may add latency vs. native drivers. | Benchmark vs. ClickHouse DBAL (if available) or native HTTP client. |
| Laravel Component | Integration Strategy |
|---|---|
| Database Layer | Register as a secondary DB connection (config/database.php). |
| Query Builder | Extend with macros for ClickHouse-specific syntax (e.g., nativeParams()). |
| Eloquent | Use custom accessors for ClickHouse types or repository pattern. |
| Migrations | Implement custom Artisan commands for ClickHouse DDL. |
| Queues/Jobs | Offload async inserts/queries to Laravel Queues (bus:work). |
| API Layer | Use ClickHouse for analytics endpoints (e.g., /metrics, /reports). |
| Caching | Cache frequent ClickHouse queries in Redis (e.g., Cache::remember). |
DB::connection('clickhouse') for queries.$results = DB::connection('clickhouse')->select('SELECT * FROM analytics WHERE event_date > :date', ['date' => now()->subDay()]);
ClickHouse::insert('events', $batchData, ['event_time', 'user_id', 'metric']);
DB::transaction(function () {
// PostgreSQL write
User::create([...]);
// Async ClickHouse write (via queue)
dispatch(new SyncToClickHouse($user));
});
DB::purge() to reset connections.| Laravel Feature | Compatibility Notes |
|---|---|
| Eloquent Models | Works via custom accessors (e.g., getDateTime64Attribute()). |
| Migrations | Requires custom Artisan commands (ClickHouse lacks Laravel Migrations support). |
| Query Builder | Extendable via macros (e.g., DB::macro('clickhouseSelect', fn($query) => ...)). |
| Caching | Cache ClickHouse results in Redis or Laravel Cache. |
| Queues | Use Laravel Queues for async ClickHouse operations. |
| Scouting | Not applicable (ClickHouse is not a full-text search DB). |
| Events | Use Laravel Events to trigger ClickHouse syncs (e.g., user.created). |
config/database.php:
'connections' => [
'clickhouse' => [
'driver' => 'clickhouse',
'host' => env('CLICKHOUSE_HOST', '127.0.0.1'),
'port' => env('CLICKHOUSE_PORT', 8123),
'database' => env('CLICKHOUSE_DB', 'default'),
'username' => env('CLICKHOUSE_USER', 'default'),
'password' => env('CLICKHOUSE_PASSWORD', ''),
'timeout' => 10,
],
],
app/ClickHouse.php):
namespace App\Facades;
use ClickHouseDB\Client;
class ClickHouse extends \Illuminate\Support\Facades\Facade {
protected static function getFacadeAccessor() { return 'clickhouse'; }
}
DB::macro('clickhouseSelect', function ($query, $bindings = []) {
return app('clickhouse')->select($query, $bindings);
});
class SyncToClickHouse implements ShouldQueue {
public function handle() {
ClickHouse::insert('events', $this->data, ['event_time', 'user_id']);
}
}
try {
$result = ClickHouse::select('SELECT ...');
} catch (\ClickHouseDB\Exception $e) {
Log::error("ClickHouse Error: " . $e->getMessage());
throw $e;
}
| Task | Effort Level | Notes |
|---|---|---|
| Dependency Updates | Medium | Monitor smi2/phpclickhouse for PHP 8.2+ compatibility. |
| Schema Changes | High | ClickHouse lacks migrations; use custom scripts or ** |
How can I help you explore Laravel packages today?