Can i create a migration for multiple tables with same columns in laravel?

Viewed 707

I need to create multiple tables with the same columns like both pens, pencil tables have the same columns. Is it possible to make the migration in a single function for multiple tables in laravel ?

pens

  • name
  • description
  • price
  • source
  • processing_started_at
  • processing_ended_at

pencils

  • name
  • description
  • price
  • source
  • processing_started_at
  • processing_ended_at
1 Answers

A migration is generic PHP code so you can do pretty much whatever you want in it. You're not even required to do anything to the database in a migration (though you probably should).

In your case something like below will work:

public function up() {
    foreach (['pen', 'pencil'] as $tableName) {
         Schema::create($tableName, function (Blueprint $table) {
              $table->string('name');
              $table->text('description');
              // and so on
         });
    }
  
}
public function down() {
    foreach (['pen', 'pencil'] as $tableName) {
         Schema::dropIfExists($tableName);
    }
}
Related