Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

PHP Laravel 4 Basics Laravel and Databases Migrations & Schema Builder

Updating database.

In the video it shows how to rollback a migration, make a change then re migrate.

How do you update a table without deleting all the data, ie. change 'name' column in the db to 'title' but still keep all the same info in the column?

2 Answers

Mike Costa
PLUS
Mike Costa
Courses Plus Student 26,362 Points

If you are using Laravel Migrations, you can create a new migration and inside of that you would use the Schema Builder Laravel provides.

Inside the up function, you would do:

Schema::table('TABLE NAME GOES HERE', function($table)
{
    $table->renameColumn('name', 'title');
});

And inside the down function, you would put the opposite of what you did in the up function:

Schema::table('TABLE NAME GOES HERE', function($table)
{
    $table->renameColumn('title', 'name');
});

This way if you need to use the rollback functionality, this is a clean way to do it.

Thanks that was what I was looking for :)

Hey, I have just got around to trying this and nothing happens. Do you know what I could be doing wrong? On the laravel site it says "Before renaming a column, be sure to add the doctrine/dbal dependency to your composer.json file." But isn't this automatically added with homestead?

<?php

use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class UpdateTodoListsTable extends Migration {

    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::table('todo_lists', function($table)
        {
            $table->renameColumn('name', 'title');
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::table('todo_lists', function($table)
        {
            $table->renameColumn('title', 'name');
        });
    }

}

Hi Robert, if you are using that framework according to its documentation you have to add it on your own. The documentation says....

"To rename a column, you may use the renameColumn method on the Schema builder. Before renaming a column, be sure to add the doctrine/dbal dependency to your composer.json file."

Schema::table('users', function($table)
{
    $table->renameColumn('from', 'to');
});

If you are unsure of how to do that. You can check this out, it seems pretty helpful.

Mike Costa
Mike Costa
Courses Plus Student 26,362 Points

After you make the migration, you still have to migrate it by using php artisan migrate.

You can do that with... "ALTER TABLE....". I am not sure if you are using MySQL or something else, but you can check the documentation of it and see how it can be written. For example if you are using MYSQL and your table name is "students" you could do that by using this command "alter table students change name title varchar (40) ;" If you are using some other means to create the database, check its documentation to see how you can alter a table.