Skip to content

[12.x] Add tsvector column type for PostgreSQL#59004

Merged
taylorotwell merged 2 commits into
laravel:12.xfrom
milroyfraser:feature/tsvector-column-type
Feb 27, 2026
Merged

[12.x] Add tsvector column type for PostgreSQL#59004
taylorotwell merged 2 commits into
laravel:12.xfrom
milroyfraser:feature/tsvector-column-type

Conversation

@milroyfraser

Copy link
Copy Markdown
Contributor

Summary

Adds a tsvector() column type method to Blueprint and the corresponding type handler in PostgresGrammar. This allows creating PostgreSQL tsvector columns using the fluent schema builder instead of raw DB::statement() calls.

Motivation

PostgreSQL's tsvector type is essential for full-text search. Currently, creating tsvector columns (especially generated ones with storedAs()) requires raw SQL:

DB::statement("
    ALTER TABLE articles
    ADD COLUMN search_vector tsvector
    GENERATED ALWAYS AS (
        setweight(to_tsvector('english', coalesce(title, '')), 'A') ||
        setweight(to_tsvector('english', coalesce(body, '')), 'B')
    ) STORED
");

With this PR, the same can be expressed fluently:

Schema::table('articles', function (Blueprint $table) {
    $table->tsvector('search_vector')->nullable()->storedAs(
        "setweight(to_tsvector('english', coalesce(title, '')), 'A') || " .
        "setweight(to_tsvector('english', coalesce(body, '')), 'B')"
    );
});

This follows the same pattern as the existing vector() column type.

Changes

  • Blueprint::tsvector() — adds column of type tsvector
  • PostgresGrammar::typeTsvector() — returns the tsvector SQL type string
  • 3 new test cases in DatabasePostgresSchemaGrammarTest

🤖 Generated with Claude Code

milroyfraser and others added 2 commits February 25, 2026 21:50
Adds a `tsvector()` column type method to Blueprint and the
corresponding type handler in PostgresGrammar. This allows creating
PostgreSQL tsvector columns using the fluent schema builder instead
of raw DB::statement() calls.

Usage:
$table->tsvector('search_vector')->nullable()->storedAs(
    "setweight(to_tsvector('dutch', coalesce(name, '')), 'A') || " .
    "setweight(to_tsvector('dutch', coalesce(description, '')), 'B')"
);

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
@taylorotwell taylorotwell merged commit 277daca into laravel:12.x Feb 27, 2026
70 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants