singlestoredb/singlestoredb-laravel
Official SingleStoreDB driver for Laravel. Wraps Laravel’s MySQL support to improve compatibility and add SingleStore features: Eloquent/migration extensions (columnstore/rowstore, shard/sort keys, etc.), JSON column support, query fixes, and tested across PHP/Laravel versions.
Installation:
composer require singlestoredb/singlestoredb-laravel
Ensure pdo_mysql is enabled (php -i | grep pdo_mysql).
Configure Database:
Update config/database.php to use the singlestore driver:
'singlestore' => [
'driver' => 'singlestore',
'host' => env('DB_HOST'),
'database' => env('DB_DATABASE'),
'username' => env('DB_USERNAME'),
'password' => env('DB_PASSWORD'),
'charset' => 'utf8mb4',
'collation' => 'utf8mb4_unicode_ci',
'options' => [
PDO::MYSQL_ATTR_SSL_CA => env('MYSQL_ATTR_SSL_CA'),
PDO::ATTR_EMULATE_PREPARES => true,
PDO::ATTR_PERSISTENT => true, // Recommended for performance
],
],
Set default to 'singlestore' in the same file.
First Use Case: Run a migration to test the connection:
php artisan migrate
Example migration:
Schema::create('users', function (Blueprint $table) {
$table->id();
$table->string('name');
$table->timestamps();
});
Migrations with SingleStore-Specific Features: Leverage SingleStore’s unique table types and optimizations:
Schema::create('analytics', function (Blueprint $table) {
$table->columnstore(); // Default, but explicit for clarity
$table->sortKey('created_at'); // Optimize for time-series queries
$table->json('metadata'); // Native JSON support
});
Eloquent Model Integration: Use Eloquent as usual, but with SingleStore’s optimizations:
class User extends Model {
protected $connection = 'singlestore';
protected $casts = [
'options' => 'json', // Native JSON casting
];
}
Query Optimization:
PDO::ATTR_PERSISTENT in config for transaction-heavy apps.Schema::create('orders', function (Blueprint $table) {
$table->shardKey('user_id'); // Shard by user_id
});
Temporary Tables: Use global temporary tables for session-specific data:
Schema::create('temp_data', function (Blueprint $table) {
$table->rowstore()->temporary()->global();
});
JSON Columns: Store and query JSON natively:
$user = User::find(1);
$user->metadata->push(['key' => 'value']); // Mutate JSON
$user->save();
Full-Text Search: Enable FULLTEXT indexes for search:
Schema::table('articles', function (Blueprint $table) {
$table->fullText('title', 'content');
});
config/queue.php to use SingleStore:
'failed' => [
'driver' => 'database-uuids',
'database' => 'singlestore',
'table' => 'failed_jobs',
],
PDO::MYSQL_ATTR_SSL_CA => '/path/to/singlestore_bundle.pem',
ORDER BY in Updates/Deletes: Configure the driver to ignore it:
'ignore_order_by_in_updates' => true,
'ignore_order_by_in_deletes' => true,
Persistent Connections:
DB::transaction() and ensure rollbacks in finally blocks.ORDER BY in Updates/Deletes:
ORDER BY in UPDATE/DELETE. Ignoring it may delete/update random rows if LIMIT/OFFSET is used.PHP < 8.1:
PDO::ATTR_EMULATE_PREPARES returns numeric values as strings.$casts to convert types.JSON Column Quirks:
->>) may not work as expected in raw queries.$casts or SingleStore’s JSON functions in raw queries:
DB::select("SELECT JSON_EXTRACT(column, '$.key') FROM table");
Shard Key Misconfiguration:
ANALYZE TABLE and adjust based on query patterns.Temporary Tables:
DB::statement("DROP TEMPORARY TABLE IF EXISTS temp_data");
Connection Issues:
PDO::MYSQL_ATTR_SSL_CA).pdo_mysql is enabled (php -m | grep pdo_mysql).DB::enableQueryLog() to inspect generated SQL:
DB::connection('singlestore')->enableQueryLog();
User::all(); // Trigger query
dd(DB::getQueryLog());
Query Performance:
EXPLAIN:
DB::select("EXPLAIN SELECT * FROM users WHERE id = ?", [1]);
SHOW STATUS LIKE 'Com_%' for connection metrics.Custom Blueprint Methods:
Extend the Blueprint class to add SingleStore-specific methods:
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
Schema::extend(function (Blueprint $blueprint) {
$blueprint->macro('customMethod', function () {
// Add SingleStore-specific logic
});
});
Query Builder Modifications: Override the query builder to handle SingleStore syntax:
DB::connection('singlestore')->setQueryGrammar(
new \SinglestoreDB\Laravel\Grammars\SinglestoreGrammar
);
Event Listeners: Hook into SingleStore events (e.g., after table creation) to automate optimizations:
Schema::after(function (Blueprint $blueprint, $table) {
if ($table === 'users') {
DB::statement("ALTER TABLE users ADD INDEX idx_name (name)");
}
});
Service Provider: Bind SingleStore-specific interfaces in a service provider:
$this->app->bind(
\SinglestoreDB\Laravel\Contracts\Optimizer::class,
\SinglestoreDB\Laravel\Optimizers\ColumnstoreOptimizer::class
);
columnstore_segment_rows: Tune sort keys for analytical workloads:
$table->sortKey('date')->with(['columnstore_segment_rows' => 50000]);
sparse Columns: Reduce storage for low-cardinality columns:
$table->string('status')->sparse(); // Only stores non-null values
DB::table('users')->insert([
['name' => 'Alice'], ['name' => 'Bob'],
]);
SHOW ENGINE: Check table status:
DB::select("SHOW ENGINE table_name STATUS");
How can I help you explore Laravel packages today?