colopl/laravel-tidb
A Laravel database driver for TiDB, the MySQL-compatible distributed SQL database. It extends Laravel's MySQL grammar with TiDB-specific schema support such as AUTO_RANDOM columns, clustered indexes, and TiDB-aware migrations, so Eloquent and the query builder work naturally on TiDB clusters.
composer require colopl/laravel-tidb. The service provider is auto-discovered, so no manual registration is required on modern Laravel.config/database.php using 'driver' => 'tidb'. Copy the standard mysql block (host, port — typically 4000 for TiDB — database, username, password, charset, collation). The driver name is the only meaningful change; it is what swaps in the package's TiDB connection and schema grammar.DB::connection('tidb') at it and proceed as you would on MySQL.pingcap/tidb Docker image or TiDB Cloud) and run php artisan migrate to validate that your schema builder calls survive TiDB DDL.tidb as the canonical connection for both the app and the test suite. Keep a separate mysql connection only when you genuinely target MySQL as well.Schema::create / Schema::table normally. The package's grammar adapts the emitted DDL to TiDB; you should not need to hand-write DDL for standard column/index definitions.AUTO_RANDOM or shard_row_id_bits to avoid auto-increment hotspots across regions. Laravel does not emit AUTO_RANDOM natively — either subclass the grammar (see extension points below) or issue raw DDL inside the migration.mysql connection for reporting/legacy workloads and tidb for the OLTP path; select per model with protected $connection = 'tidb';.pingcap/tidb (plus PD/TiKV, or a single-node tidb image for quick iteration). Match the charset/collation to production to avoid surprises.php artisan migrate returns before DDL necessarily completes in TiDB — DDL is online and asynchronous. For release gating, poll ADMIN SHOW DDL JOBS rather than assuming the schema is live the moment the command exits.'driver' => 'tidb'. Falling back to the plain mysql driver bypasses the package's grammar and re-exposes TiDB to unsupported MySQL DDL.FULLTEXT or SPATIAL indexes. Audit migrations for $table->fullText() and $table->spatialIndex() before shipping.->change() / column modifications. TiDB's ALTER TABLE … MODIFY semantics differ from MySQL 8. The package handles common cases, but compound changes (multiple columns, type + default + collation in one call) may still need raw SQL.tidb_enable_foreign_key is set. Do not rely on them for integrity across all deployments — enforce at the application layer or via constraints you control.DB::transaction() maps fine, but automatic retry on write conflicts (optimistic mode) is not built in — wrap contended writes in an explicit retry loop.illuminate/* constraints and may drop Laravel 10/11 — verify composer.json before upgrading a running app, and note Laravel 12 requires PHP 8.2+.SHOW CREATE TABLE \table`` on TiDB to see exactly what the grammar emitted and diff it against what MySQL would produce. Use the TiDB slow query log for query-level issues.charset and collation consistent with the cluster (utf8mb4). Mismatches usually surface as comparison or connection errors, not DDL errors, which makes them easy to misattribute to the package.AUTO_RANDOM), then rebind via a custom resolver in a service provider.if (DB::connection()->getDriverName() === 'tidb') blocks or Schema::hasTable checks to keep migrations idempotent across drivers.How can I help you explore Laravel packages today?