Skip to content

Added "after" support for morphs and nullableMorphs Blueprint #56613

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Aug 11, 2025

Conversation

marcogermani87
Copy link
Contributor

Added support for the after option to the morphs and nullableMorphs Blueprint methods.

Benefit: This allows you to add polymorphic relationships to an existing database table and precisely control the placement of the new columns (morphable_id and morphable_type). This eliminates the need for manual SQL commands to position the columns correctly after they're added.

Added after supporto for morphs and nullableMorphs Blueprint
Fixed PHPDocs refs
@negoziator
Copy link
Contributor

@marcogermani87

Wow. I have litterally just thought about this 10 minutes ago, because i wanted to do exactly this - but had to make the two fields individually instead ...

You have my vote at least ⭐ 🚀

image

@marcogermani87
Copy link
Contributor Author

@marcogermani87

Wow. I have litterally just thought about this 10 minutes ago, because i wanted to do exactly this - but had to make the two fields individually instead ...

You have my vote at least ⭐ 🚀

image

Thanks! I'm reviewing this PR, but I noticed there are some test errors.

Also, remember to add an index to your manual implementations:

$table->index(["paymentable_type", "paymentable_id"], 'paymentable_idx');

@negoziator
Copy link
Contributor

@marcogermani87 Good catch! 💯 Thanks! ⭐

@marcogermani87
Copy link
Contributor Author

@negoziator Good news! All checks passed, and the branch is ready for merge. Hope to see it merged soon! 🚀🚀🚀

@marcogermani87
Copy link
Contributor Author

marcogermani87 commented Aug 11, 2025

@marcogermani87 Good catch! 💯 Thanks! ⭐

In my case, I've implemented this:

<?php

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

return new class extends Migration
{
    private string $morphName = 'commentable';

    public function up()
    {
        Schema::table('posts', function (Blueprint $table) {
            $table->after('id', function (Blueprint $table) {
                $table->string("{$this->morphName}_type");
                $table->unsignedBigInteger("{$this->morphName}_id");
            });
            $table->index(["{$this->morphName}_type", "{$this->morphName}_id"], "idx_{$this->morphName}");
        });
    }

    /**
     * Reverse the migrations.
     */
    public function down(): void
    {
        Schema::table('posts', function (Blueprint $table) {
            $table->dropMorphs($this->morphName, "idx_{$this->morphName}");
        });
    }
};

@taylorotwell taylorotwell merged commit 0d09b30 into laravel:12.x Aug 11, 2025
60 checks passed
@marcogermani87 marcogermani87 deleted the patch-1 branch August 11, 2025 15:10
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.

3 participants