Installation
To upgrade from an earlier version, see Upgrade.
Install the package
Install the package with Composer:
shcomposer require oralunal/laravel-clickhouseSet the connection values in
.env:dotenvCLICKHOUSE_HOST=localhost CLICKHOUSE_PORT=8123 CLICKHOUSE_DATABASE=default CLICKHOUSE_USERNAME=default CLICKHOUSE_PASSWORD= # Only for an HTTPS connection: CLICKHOUSE_HTTPS=true
Laravel finds the service provider through package auto-discovery. The provider adds the clickhouse connection to config('database.connections'). A single-node setup needs no other change.
If you disabled auto-discovery, add Oralunal\LaravelClickHouse\ClickhouseServiceProvider::class to bootstrap/providers.php.
Change the defaults
To change more than the .env values, publish the config file:
php artisan vendor:publish --tag=clickhouse-configThis command writes config/clickhouse.php. The values in this file override the packaged defaults. Each top-level key of the file is one connection, so you can add more connections there.
You can also define the connection in config/database.php. This definition has the highest priority:
'clickhouse' => [
'driver' => 'clickhouse',
'host' => env('CLICKHOUSE_HOST', '127.0.0.1'),
'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', false),
'retries' => env('CLICKHOUSE_RETRIES', 0),
'settings' => [
'max_partitions_per_insert_block' => 300,
],
],Configuration lists all options.
Use the connection
The connection works with Laravel's DB facade:
use Illuminate\Support\Facades\DB;
$rows = DB::connection('clickhouse')->select('SELECT * FROM summing_url_views LIMIT 2');To use the smi2/phpClickHouse client directly, get it from the connection:
/** @var \ClickHouseDB\Client $client */
$client = DB::connection('clickhouse')->getClient();
$statement = $client->select('SELECT * FROM summing_url_views LIMIT 2');For the client API, see the smi2/phpClickHouse README.
Laravel's query builder works on the connection, with the ClickHouse clauses of the package:
DB::connection('clickhouse')->table('summing_url_views')->final()->where('url', 'like', '%/docs%')->count();
// select count(*) as "aggregate" from "summing_url_views" final where "url" like '%/docs%'Next steps
- Create an Eloquent model with
php artisan make:clickhouse-model. - Create a migration with
php artisan make:clickhouse-migration. - Query with the query builder.