Eloquent model for ClickHouse
- Laravel 5.6
- PHP 7.2
- Clickhouse Server
Add this repository into composer.json:
"repositories": [
{
"url": "[email protected]:henryh/laravel-clickhouse.git",
"type": "git"
}
],
"require": {
"henryh/laravel-clickhouse": "master"
}
Then add the code above into your config/app.php file providers section
Esazykin\LaravelClickHouse\ClickHouseServiceProvider::class,
And add new connection into your config/database.php file. Something like this:
'connections' => [
'clickhouse' => [
'driver' => 'clickhouse',
'host' => env('CLICKHOUSE_HOST', 'localhost'),
'port' => env('CLICKHOUSE_PORT', '8123'),
'database' => env('CLICKHOUSE_DATABASE', 'test'),
'username' => env('CLICKHOUSE_USERNAME', 'default'),
'password' => env('CLICKHOUSE_PASSWORD', 'default'),
'options' => [
'timeout' => 10,
'protocol' => 'https'
]
]
]
Add settings into your .env file:
CLICKHOUSE_DATABASE=test
CLICKHOUSE_USERNAME=default
CLICKHOUSE_PASSWORD=default
Or like this, if clickhouse runs in cluster add in config/database.php:
'connections' => [
'clickhouse' => [
'driver' => 'clickhouse',
'cluster' => [
'server-1' => [
'host' => '',
'port' => '',
'database' => '',
'username' => '',
'password' => '',
'options' => [
'timeout' => 10,
'protocol' => 'https'
]
],
'server-2' => [
'host' => '',
'port' => '',
'database' => '',
'username' => '',
'password' => '',
'options' => [
'timeout' => 10,
'protocol' => 'https'
]
]
]
]
]
Then create model
<?php
use Esazykin\LaravelClickHouse\Database\Eloquent\Model;
class Payment extends Model
{
protected $table = 'payments';
}
And use it
Payment::select(raw('count() AS cnt'), 'payment_system')
->whereBetween('payed_at', [
Carbon\Carbon::parse('2018-01-01'),
now(),
])
->groupBy('payment_system')
->get();