Lluís Puig Ferrer
Lluís Puig Ferrer

Reputation: 1146

Laravel: Add new column on DDBB without remove the data

I have this migration:

public function up()
    {
        Schema::create('clients', function(Blueprint $table)
        {
            $table->increments('id');
            $table->string('name');
            $table->string('slug');
            $table->timestamps();
        });
    }

And now I want to add a new column, and looks like this:

public function up()
        {
            Schema::create('clients', function(Blueprint $table)
            {
                $table->increments('id');
                $table->string('name');
                $table->string('slug');
                $table->string('pathlogo')->default('/');
                $table->timestamps();
            });
        }

How can I make just an 'add column' on Laravel? I don't want to do php artisan migrate:refresh, or restart and then make again seed.

Now I have some data in DB which not exist on seed I just want to make a new column.

Upvotes: 0

Views: 48

Answers (2)

Roots
Roots

Reputation: 208

Create a new migration and Use that as your up function:

public function up()
{
    Schema::table('clients', function (Blueprint $table) {
        $table->string('pathlogo')->default('/');
    });
}

them migrate as normal.

Upvotes: 0

Jeff
Jeff

Reputation: 25221

You need to change Schema::create to Schema::table (because you are not creating a table, just selecting it), and then your only line in that function should be:

$table->string('pathlogo')->default('/')->after('slug');

after will ensure the column is positioned how you want.

If you are still in development, ie. you don't have data in the table, you should just rollback all your migrations and edit the original.

Upvotes: 2

Related Questions