Framework: Blueprint morphs() does not accept nullable()

Created on 20 May 2016  路  10Comments  路  Source: laravel/framework

Hi, when I want to make morphs that can be nullable, I try:

$table->morphs('parent')->nullable();

But this doesn't work. So I have to use:

$table->string('parent_type')->nullable();
$table->integer('parent_id')->unsigned()->nullable();

$table->index(['parent_type', 'parent_id']);

In some situations, we need nullable polymorphic relationships. For example, an image can belong to a post, an album, a comment, but it may also not belong to any parent objects. When users deleted the parent post, they might still want to keep its images. Instead of deleting image records, only set parent to null.

Most helpful comment

@turtlegood this functionality added to the core, so there is no need to add it:
https://github.com/laravel/framework/blob/3bf6296d8d4dd82308a9272edc5c4aeebf4507f6/src/Illuminate/Database/Schema/Blueprint.php#L1155-L1169

All 10 comments

As a temporary solution you can make a model (Empty or Default ...) that will be assigned when you don't want your image model to belong to a parent.

Required to me for case when record could be posted by guests, users or organizations. For users and organizations it uses related id and type, for guests it has null values.

Done it the same way described above:

$table->integer('owner_id')->unsigned()->nullable();
$table->string('owner_type')->nullable();

$table->index(['entity_id', 'owner_type']);

I found two previous issues/PRs where the nullable option had been proposed and and rejected by @taylorotwell, https://github.com/laravel/framework/pull/3123 and https://github.com/laravel/framework/issues/2815, so I'd say that it's not really something he's interested in for morphs().

That said, I'm happy to do a PR, if he does change his mind. :smile:

I agree. I think it should be possible to make a morph relation nullable thru morphs().

Since that was discussed in PRs and rejected several times I think it's better to use this in your application.

$table->string('parent_type')->nullable();
$table->integer('parent_id')->unsigned()->nullable();

Closing since it's not actually a bug report, but please feel free to continue discussions in the internals repo https://github.com/laravel/internals repo.

Here is my way. Hope it helps!

        Blueprint::macro('nullableMorphs', function ($name, $indexName = null) {
            $this->string("{$name}_type")->nullable();
            $this->unsignedBigInteger("{$name}_id")->nullable();
            $this->index(["{$name}_type", "{$name}_id"], $indexName);
        });

@turtlegood this functionality added to the core, so there is no need to add it:
https://github.com/laravel/framework/blob/3bf6296d8d4dd82308a9272edc5c4aeebf4507f6/src/Illuminate/Database/Schema/Blueprint.php#L1155-L1169

@antonkomarev wow thanks!

you can use $table->nullableMorphs() for that.

@emadha It's already mentioned above. And at the time of this issue being created, it was not a part of Laravel.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

iivanov2 picture iivanov2  路  3Comments

YannPl picture YannPl  路  3Comments

progmars picture progmars  路  3Comments

RomainSauvaire picture RomainSauvaire  路  3Comments

Anahkiasen picture Anahkiasen  路  3Comments