21

How can I create migration file from existing model in Laravel or model from existing migration?

1

4 Answers 4

10

To create a table for a existing Model, you just have to run below command,

php artisan make:migration table_name

and, go to the database -> migration -> date_with-table_name

inside up(), type your table name and fields that you want to add, something like belowa;

Schema::create('table_name', function (Blueprint $table) {
    $table->increments('id');
    $table->string('field_2');
    $table->string('field_3');
    $table->string('field_4');
    $table->date('field_5');
    $table->string('field_6');
    $table->timestamps();
});

read more here.

Sign up to request clarification or add additional context in comments.

Comments

4

For the model, just create a model, there isn't anything special, just make sure to set the $table if not following convention.

For generating migrations from an existing Database schema there is a package:

Xethron - Laravel Migration Generator

Comments

4

Model and migration are merely connected through naming convention. Models and migrations are created separately.

Comments

2
 php artisan make:migration create_users_table

your Model name will be User and the table name will be users

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.