embedding-mysql-plugin maintained by x-laravel
X-Laravel/Embedding — MySQL 9 Plugin
MySQL HeatWave native vector driver for x-laravel/embedding.
How It Works
- Implements
SimilarityDriver— registers as themysqldriver, similarity search runs entirely in MySQL usingVEC_DISTANCE_COSINE - Implements
VectorStore— writes embeddings viaINSERT ... ON DUPLICATE KEY UPDATEwithSTRING_TO_VECTOR(), reads via aVECTOR_TO_STRINGglobal scope
Requirements
- PHP ^8.3
- Laravel ^12.0 | ^13.0
x-laravel/embedding ^1.0- MySQL HeatWave —
VEC_DISTANCE_COSINEis not available in MySQL Community Edition; this plugin requires MySQL HeatWave
Installation
composer require x-laravel/embedding-mysql-plugin
The MysqlEmbeddingServiceProvider is auto-discovered and registers the mysql driver automatically.
Setup
1. Configure x-laravel/embedding
Publish the config if you haven't already:
php artisan vendor:publish --tag=embedding-config
Set the similarity driver and database connection in config/embedding.php:
'database' => [
'connection' => env('EMBEDDINGS_DATABASE_CONNECTION', env('DB_CONNECTION', 'mysql')),
'table' => env('EMBEDDINGS_DB_TABLE', 'embeddings'),
],
'similarity' => [
'driver' => env('EMBEDDING_SIMILARITY_DRIVER', 'auto'),
],
2. Create the embeddings table
This plugin ships its own MySQL-native migration that replaces the default one from x-laravel/embedding. It creates a VECTOR(1536) column.
Run the migration:
php artisan migrate
If you need to customise the DDL, publish the migration first:
php artisan vendor:publish --tag=embedding-mysql-migrations
php artisan migrate
Note:
VEC_DISTANCE_COSINEis only available on MySQL HeatWave, not MySQL Community Edition. For production, add aVECTOR INDEXon thevectorcolumn after publishing the migration.
3. Model
Follow the standard x-laravel/embedding setup. No MySQL-specific changes are needed on your models.
use XLaravel\Embedding\Attributes\EmbedOn;
use XLaravel\Embedding\Concerns\Embeddable;
use XLaravel\Embedding\Contracts\HasEmbeddings;
#[EmbedOn(['title', 'body'])]
class Post extends Model implements HasEmbeddings
{
use Embeddable;
public function toEmbeddingText(): string
{
return $this->title.' '.$this->body;
}
}
Usage
The driver is transparent — use the standard x-laravel/embedding API:
Post::similarToText('web framework', limit: 10);
Post::similarTo($vector, limit: 10, threshold: 0.8);
Post::rankByRelevance($posts, 'web framework');
$post->mostSimilar(limit: 5);
$post->similarityTo($otherPost);
All methods set a similarity_score float attribute on each returned model.
Testing
Tests require a MySQL HeatWave instance. The docker-compose.yml uses mysql:9.1.0 for CI convenience but VEC_DISTANCE_COSINE tests will fail against Community Edition — run them against a real HeatWave instance.
# Build first (once per PHP version)
DOCKER_BUILDKIT=0 docker compose --profile php83 build
# Run tests
docker compose --profile php83 up
docker compose --profile php84 up
docker compose --profile php85 up
License
This package is open-sourced software licensed under the MIT license.