Search code examples
phpmysqllaravel-5

What are the uses of morphs column type in Laravel?


What kind of data can be stored with $table->morphs column type?

$table->morphs('taggable');

I do not fully understand the explationation given here.

Adds taggable_id UNSIGNED BIGINT and taggable_type VARCHAR equivalent columns.

Thanks in advance.


Solution

  • $table->morphs('column_name') is a shortcut, it will create 2 columns:

    column_name_id with unsigned integer datatype and

    column_name_type with string datatype to create polymorphic relationship

    and index called table_name_column_name_type

    so

    $table->morphs('taggable');
    

    is the same as:

    $table->unsignedInteger("taggable_id");
    $table->string("taggable_type");
    $this->index(["taggable_type", "taggable_id"]);