Reputation: 11198
I had a migration called projects_table
that ran
Schema::create('projects', function(Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->integer('user_id');
});
I needed to add a new column called project_settings
. I assumed I could just add
$table->text('project_settings');
and run php artisan migrate
, but I was wrong, didn't work. I then read http://laravel.com/docs/5.0/schema#adding-columns and learned I have to use Schema::table(...);
so I changed my existing migration (projects_table) to:
Schema::table('projects', function(Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->text('project_settings');
$table->integer('user_id');
});
and that still did not work. I finally got it to work by creating a new migration ProjectsTableNewField
and simply adding
Schema::table('projects', function(Blueprint $table) {
$table->text('project_settings');
});
My question is, is that the right way to do this in laravel? Adding a column is a pretty basic task and If I have to create a migration for every database change, I think that migration folder will get long quick. What is the right way to do this?
edit: I also see it is important to have a created_at
and updated_at
field for migration purposes. I will add those fields as well
Upvotes: 1
Views: 1505
Reputation: 571
Very Simply you can add new columns to existing tables
php artisan make:migration new_column_in_my_table --table=your_desired_table_to_modify
and vary carefully write the name of same table you want to modify at the end of your command like:
--table=your_desired_table_to_modify
then fully command:
php artisan make:migration new_column_in_my_table table=your_desired_table_to_modify
then new created migration "new_column_in_my_table" will be in your case like:
Schema::table('projects', function(Blueprint $table) {
$table->text('project_settings')->after('name');
});
save the changes and run the command
php artisan migrate
Upvotes: 1
Reputation: 22882
This is normal behavior, but usually you only do this type of migrations when your application is already in production, or if the data of your testing website is important and you don't want to lose it.
When you are developing and the data you have isn't important you could just change your migrations, just like you did here:
Schema::table('projects', function(Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->text('project_settings');
$table->integer('user_id');
});
Then just re-run the migrations doing a refresh:
php artisan migrate:refresh
This will reset all your migrations and run them again.
Also if you are using seeders, you can run all your migrations and seed your database with just one command:
php artisan migrate:refresh --seed
Upvotes: 3
Reputation: 405
You should have a database migration file that is organized similar to the following:
public function up()
{
Schema::create('table_name', function(Blueprint $table)
{
$table->increments('id');
//custom fields here
$table->timestamps();
});
}
public function down()
{
Schema::drop('table_name');
}
Create the table initially by running the following command:
php artisan migrate
Once you make a change to the custom fields you want, run the following command:
php artisan migrate:refresh
This will run the down function in sequence of every migration and then re-create them all again with your custom fields. This keeps you from having to manage a ton of migration files. You never want to use this on a production server though, for obvious reasons.
Think through your data modeling before getting too deep into your php programming and you will thank yourself in the long run both from an organizational stand point and so that you don't have to go back and redo your Database over and over.
Keep in mind that you need to utilize seeders if you want to have sample data to work with. Your Tables are wiped every time you do this. If you're making a new table and aren't modifying previous tables, then you can run just the first command.
To use other commands, use the php artisan
command to view what is available.
Upvotes: 1
Reputation: 4694
Yes, creating a new migration when you want to update the structure of the table is the right thing to do.
It allows you to "push" this modifications on a live server without breaking the existing database.
Their's another way of doing it, but it might no be applicable if you already stored datas in your tables :
php artisan migrate:reset
php artisan migrate
Keep in mind that this is going to delete all your existing tables, ending up in a loss of datas in case you already stored some.
Upvotes: 1