Get expert guidance in a few days with a Laravel code review

Laravel ClickHouse: A Full-Featured ClickHouse Driver for Laravel

Published on by

Laravel ClickHouse: A Full-Featured ClickHouse Driver for Laravel image

Laravel ClickHouse is a database driver that integrates ClickHouse with Laravel, including Eloquent, the Query Builder, Schema Builder, and more:

  • Eloquent models with non-incrementing ID support
  • Query Builder with ClickHouse-specific clauses (i.e.,FINAL, ARRAY JOIN, SAMPLE)
  • Schema Builder with ENGINE, PARTITION BY, ORDER BY, and LowCardinality column types
  • Laravel migration support via artisan migrate
  • Concurrent query execution using Guzzle's async HTTP pool
  • Dual HTTP transport options: Guzzle and Curl/phpclickhouse

ClickHouse is an open-source column-oriented database built for analytical workloads. It stores data by column rather than by row, making aggregations over large datasets fast—capable of querying billions of rows in seconds. It's a common choice for event tracking, time-series data, and analytics dashboards where read performance at scale is the priority.

Eloquent Models

You can define Eloquent models pointing at ClickHouse the same way you would for any other database connection:

class Event extends Model
{
protected $connection = 'clickhouse';
}
 
$events = Event::where('user_id', 1)->get();

ClickHouse doesn't use auto-incrementing primary keys, so the driver configures models with non-incrementing IDs by default. Scopes and collections work as expected.

Query Builder with ClickHouse Extensions

The Query Builder covers standard Laravel methods and adds ClickHouse-specific clauses. The final parameter applies the FINAL modifier to a query, which forces ClickHouse to merge duplicate rows at read time—useful with the ReplacingMergeTree engine:

$events = DB::connection('clickhouse')
->table('events', final: true)
->where('user_id', 1)
->get();

Other extensions include PREWHERE (ClickHouse's pre-filter for primary key columns), ARRAY JOIN, SAMPLE, LIMIT BY, and SEMI/ANTI/ASOF join types.

Schema Builder and Migrations

The Schema Builder supports ClickHouse DDL via a ClickHouseBlueprint, letting you define table engines, partition keys, order keys, and column types like LowCardinality:

Schema::connection('clickhouse')->create('events', function (ClickHouseBlueprint $table) {
$table->engine('MergeTree()');
$table->orderBy(['id', 'created_at']);
$table->partitionBy('toYYYYMM(created_at)');
});

Standard artisan migrate commands work with a ClickHouse-compatible migration repository, so you can manage schema changes alongside your other databases.

Concurrent Query Execution

The package includes a Parallel helper that runs multiple queries at the same time using Guzzle's async HTTP pool:

$results = Parallel::get([
'users' => User::where('active', 1),
'events' => Event::where('type', 'click'),
]);

Both users and events execute concurrently, and the results are returned as a keyed array once all queries resolve.

You can find the full documentation and source on GitHub.

Paul Redmond photo

Staff writer at Laravel News. Full stack web developer and author.

Cube

Laravel Newsletter

Join 40k+ other developers and never miss out on new tips, tutorials, and more.

image
Acquaint Softtech

Hire Laravel developers with AI expertise at $20/hr. Get started in 48 hours.

Visit Acquaint Softtech
Tinkerwell logo

Tinkerwell

The must-have code runner for Laravel developers. Tinker with AI, autocompletion and instant feedback on local and production environments.

Tinkerwell
PhpStorm logo

PhpStorm

The go-to PHP IDE with extensive out-of-the-box support for Laravel and its ecosystem.

PhpStorm
SaaSykit: Laravel SaaS Starter Kit logo

SaaSykit: Laravel SaaS Starter Kit

SaaSykit is a Multi-tenant Laravel SaaS Starter Kit that comes with all features required to run a modern SaaS. Payments, Beautiful Checkout, Admin Panel, User dashboard, Auth, Ready Components, Stats, Blog, Docs and more.

SaaSykit: Laravel SaaS Starter Kit
Harpoon: Next generation time tracking and invoicing logo

Harpoon: Next generation time tracking and invoicing

The next generation time-tracking and billing software that helps your agency plan and forecast a profitable future.

Harpoon: Next generation time tracking and invoicing
Get expert guidance in a few days with a Laravel code review logo

Get expert guidance in a few days with a Laravel code review

Expert code review! Get clear, practical feedback from two Laravel devs with 10+ years of experience helping teams build better apps.

Get expert guidance in a few days with a Laravel code review
Lucky Media logo

Lucky Media

Get Lucky Now - the ideal choice for Laravel Development, with over a decade of experience!

Lucky Media
Shift logo

Shift

Running an old Laravel version? Instant, automated Laravel upgrades and code modernization to keep your applications fresh.

Shift
Kirschbaum logo

Kirschbaum

Providing innovation and stability to ensure your web application succeeds.

Kirschbaum
Laravel Cloud logo

Laravel Cloud

Easily create and manage your servers and deploy your Laravel applications in seconds.

Laravel Cloud
Acquaint Softtech logo

Acquaint Softtech

Acquaint Softtech offers AI-ready Laravel developers who onboard in 48 hours at $3000/Month with no lengthy sales process and a 100 percent money-back guarantee.

Acquaint Softtech
SerpApi logo

SerpApi

Access real-time search engine results through a simple API—no more scraping headaches! Use it for AI applications, SEO tools, product research, travel information, and more

SerpApi

The latest

View all →
Laravel Installer Now Returns JSON When Running Inside an AI Agent image

Laravel Installer Now Returns JSON When Running Inside an AI Agent

Read article
Queue-Wide Inspection Methods in Laravel 13.8.0 image

Queue-Wide Inspection Methods in Laravel 13.8.0

Read article
Verifiable Audit Logging with Laravel Chronicle image

Verifiable Audit Logging with Laravel Chronicle

Read article
Ship AI with Laravel: Search Entire PDFs with Zero Search Logic image

Ship AI with Laravel: Search Entire PDFs with Zero Search Logic

Read article
Personalized Content Delivery System: Building an AI-powered recommendation engine with Laravel and MongoDB image

Personalized Content Delivery System: Building an AI-powered recommendation engine with Laravel and MongoDB

Read article
Laravel Brain: Visualize Your Application's Request Lifecycle image

Laravel Brain: Visualize Your Application's Request Lifecycle

Read article