Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

ShafiqulIslam's avatar

How to refactor common columns in migration in laravel?

I have these columns which I need to use on every migration tables.

$table->string('created_by');
$table->string('updated_by')->nullable();
$table->timestamps();

I need to extract these line, so that I do not have to write them every time. How can I achieve that with laravel 5.2?

0 likes
3 replies
InaniELHoussain's avatar
Level 32
trait usedColumns {

    public function runColmuns($table)
    {
    $table->string('created_by'); $table->string('updated_by')->nullable(); $table->timestamps();
    }

}

EDIT : $table should be passed

and in your migration's class, use the trait and run the method

$this->runColmuns($table);
ShafiqulIslam's avatar

Thanks. It works after I passed the $table to the function.

1 like

Please or to participate in this conversation.