Multiple connections
config/clickhouse.php maps connection names to connection configs. The service provider adds each entry to config('database.connections.<name>').
Publish the config file:
shphp artisan vendor:publish --tag=clickhouse-configAdd a connection to
config/clickhouse.php:phpreturn [ 'clickhouse' => [ // The default connection ], 'clickhouse2' => [ 'driver' => 'clickhouse', 'host' => env('CLICKHOUSE2_HOST', '127.0.0.1'), 'port' => env('CLICKHOUSE2_PORT', '8123'), 'database' => 'default', 'username' => 'default', 'password' => '', 'timeout_connect' => 2, 'timeout_query' => 2, 'https' => false, 'retries' => 0, 'retry_on' => 'reads', 'use_lightweight_delete' => false, ], ];Set the connection of a model:
phpnamespace App\Models\Clickhouse; use Oralunal\LaravelClickHouse\Eloquent\Model; class MyTable2 extends Model { protected $connection = 'clickhouse2'; protected $table = 'my_table2'; }Set the connection of a migration:
phpuse Illuminate\Database\Migrations\Migration; use Illuminate\Support\Facades\Schema; use Oralunal\LaravelClickHouse\SchemaBlueprint; return new class extends Migration { protected $connection = 'clickhouse2'; public function up(): void { Schema::connection($this->connection)->create('my_table2', function (SchemaBlueprint $table) { $table->unsignedInteger('id'); $table->string('field_one'); $table->orderBy('id'); }); } public function down(): void { Schema::connection($this->connection)->dropIfExists('my_table2'); } };Query the connection:
phpDB::connection('clickhouse2')->table('my_table2')->where('id', 1)->get(); MyTable2::where('id', 1)->get();
The priority, from highest: the connections of config/database.php, then config/clickhouse.php, then the packaged defaults. The packaged defaults apply to the clickhouse connection only. Give a new connection all its options, at least database.
To query two connections at the same time, see Parallel queries.