Skip to content

This page describes 1.x, an earlier version. The latest version is 4.x: read its documentation or upgrade to 4.x.

Installation ​

Install the package ​

sh
composer require oralunal/phpclickhouse-laravel:^1.5

From 1.1.0, Laravel finds the service provider through package auto-discovery. When auto-discovery is off, or on 1.0.0, add PhpClickHouseLaravel\ClickhouseServiceProvider::class to bootstrap/providers.php.

Configure the connection ​

For one server, set these values in .env:

dotenv
CLICKHOUSE_HOST=localhost
CLICKHOUSE_PORT=8123
CLICKHOUSE_DATABASE=default
CLICKHOUSE_USERNAME=default
CLICKHOUSE_PASSWORD=
# Only for an HTTPS connection
CLICKHOUSE_HTTPS=true

The service provider merges the default values into config('database.connections.clickhouse'). You do not have to change a config file.

To change other defaults, publish the config file:

sh
php artisan vendor:publish --tag=clickhouse-config

The command writes config/clickhouse.php. From 1.4.0, its values replace the defaults of the package. Before 1.4.0, the package does not read the published file. You can also add more connections to it. See Multiple connections.

A connection in config/database.php replaces both:

php
'clickhouse' => [
    'driver' => 'clickhouse',
    'host' => env('CLICKHOUSE_HOST'),
    'port' => env('CLICKHOUSE_PORT', '8123'),
    'database' => env('CLICKHOUSE_DATABASE', 'default'),
    'username' => env('CLICKHOUSE_USERNAME', 'default'),
    'password' => env('CLICKHOUSE_PASSWORD', ''),
    'timeout_connect' => env('CLICKHOUSE_TIMEOUT_CONNECT', 2),
    'timeout_query' => env('CLICKHOUSE_TIMEOUT_QUERY', 2),
    'https' => (bool) env('CLICKHOUSE_HTTPS', null),
    'retries' => env('CLICKHOUSE_RETRIES', 0),
    'settings' => [ // Optional
        'max_partitions_per_insert_block' => 300,
    ],
    'fix_default_query_builder' => true,
],

Options ​

Option.env variableDefaultDescription
hostCLICKHOUSE_HOST127.0.0.1Host of the server
portCLICKHOUSE_PORT8123HTTP port of the server
databaseCLICKHOUSE_DATABASEdefaultDatabase of the connection
usernameCLICKHOUSE_USERNAMEdefaultUser name
passwordCLICKHOUSE_PASSWORD''Password
httpsCLICKHOUSE_HTTPSfalseUse HTTPS
timeout_connectCLICKHOUSE_TIMEOUT_CONNECT2Seconds to wait for a connection
timeout_queryCLICKHOUSE_TIMEOUT_QUERY2Seconds that a query can take
retriesCLICKHOUSE_RETRIES0Number of retries of a failed request. See Timeouts and retries.
settings[]ClickHouse settings for each query of the connection
fix_default_query_buildertrueDB::connection('clickhouse')->table() returns the package's query builder. See Known limitations.
clusterList of nodes. It replaces host and port. See Clusters.
cluster_nameName of a cluster of the server config, for ON CLUSTER. See Clusters.

Next steps ​

Released under the MIT License.