Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create two tables migration using php artisan command for Laravel Project?

I am using composer for migrating two tables like this:

php artisan make:migration create_two_tables --create="projects","tasks"

Its creating the file in database->migration folder.

but while migrating using

php artisan migrate

its creating only table in database like this:

'projects,tasks'

as one table.

I want only one file and only one command like what I do in the top for migrating two tables in db.

Is there any possibility to get this?

Note: My superior ordered me to not change the database migration file at any cost...

Can anyone help me out here...

like image 978
Akilsree1 Avatar asked Feb 03 '26 15:02

Akilsree1


1 Answers

The make:migration command is just to create a new migration file from a template. To actually define the migration you normally have to edit that file. So in your case you would do this:

php artisan make:migration create_two_tables --create="projects"

Then open the ***_create_two_tables.php migration file and add the second table:

/**
 * Run the migrations.
 *
 * @return void
 */
public function up()
{
    Schema::create('projects', function(Blueprint $table)
    {

    });

    Schema::create('tasks', function(Blueprint $table)
    {

    });
}

/**
 * Reverse the migrations.
 *
 * @return void
 */
public function down()
{
    Schema::drop('projects');
    Schema::drop('tasks');
}

Usually you also want to add actual columns to your tables. And that you do inside the Schema::create closure:

Schema::create('projects', function(Blueprint $table)
{
    $table->increments('id');
    $table->string('name');
    // and so on
});

Read more about creating tables with the Schema Builder here

like image 63
lukasgeiter Avatar answered Feb 06 '26 04:02

lukasgeiter