Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel Generate Migration from existing Model

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

like image 918
Ники Арсов Avatar asked Dec 05 '17 09:12

Ники Арсов


People also ask

How do you make a model and migration in Laravel in one command?

How do you make a controller model and migration in one command in Laravel? Navigate to your project folder and run the following commands to create new: Model: php artisan make:model YourModelName. Controller: php artisan make:controller YourControllerName.


2 Answers

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.

like image 105
Blasanka Avatar answered Oct 26 '22 00:10

Blasanka


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

like image 43
lagbox Avatar answered Oct 26 '22 00:10

lagbox