Skip to content

Add whenTableHasIndex and whenTableDoesntHaveIndex to Builder#58005

Merged
taylorotwell merged 3 commits into
laravel:12.xfrom
jackbayliss:add-when-table-has-index
Dec 4, 2025
Merged

Add whenTableHasIndex and whenTableDoesntHaveIndex to Builder#58005
taylorotwell merged 3 commits into
laravel:12.xfrom
jackbayliss:add-when-table-has-index

Conversation

@jackbayliss

@jackbayliss jackbayliss commented Dec 3, 2025

Copy link
Copy Markdown
Contributor

I reached for these but only found the whenTableHasColumn ones

This PR adds whenTableHasIndex and whenTableDoesntHaveIndex to align with whenTableHasColumn.

Not sure if you're keen on these, but thought i'd give it a go to help dx

I don't see any tests for whenTableHasColumn - so have left these out for now. Let me know if you want some tests? I can have a go later on.

Before you'd have to use the Schema again:

<?php

use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Migrations\Migration;

return new class extends Migration
{

    public function up(): void
    {
        if (! Schema::hasIndex('product', 'name')) {
            Schema::table('product', function (Blueprint $table) {
                $table->index('name', 'index_name');
            });
        }
    }
};

Now you can just do the below:

<?php

use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Migrations\Migration;

return new class extends Migration
{
    public function up(): void
    {
        Schema::whenTableDoesntHaveIndex('product', 'name', function (Blueprint $table) {
            $table->index('name', 'index_name');
        });

        // You can also pass the index type...
        Schema::whenTableDoesntHaveIndex('product', 'name', function (Blueprint $table) {
            $table->index('name', 'index_name');
        },  'unique');
    }
};

@github-actions

github-actions Bot commented Dec 3, 2025

Copy link
Copy Markdown

Thanks for submitting a PR!

Note that draft PR's are not reviewed. If you would like a review, please mark your pull request as ready for review in the GitHub user interface.

Pull requests that are abandoned in draft may be closed due to inactivity.

@jackbayliss jackbayliss marked this pull request as ready for review December 3, 2025 17:17
@taylorotwell

Copy link
Copy Markdown
Member

Type should be the last arg imo.

@taylorotwell taylorotwell marked this pull request as draft December 4, 2025 17:38
@jackbayliss

Copy link
Copy Markdown
Contributor Author

@taylorotwell Should be good now, thank you for your review 🫡

@jackbayliss jackbayliss marked this pull request as ready for review December 4, 2025 17:57
@taylorotwell taylorotwell merged commit 715f60e into laravel:12.x Dec 4, 2025
74 checks passed
akyrey pushed a commit to akyrey/framework that referenced this pull request Dec 29, 2025
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