Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel 4 Artisan drop existing column from table

Tags:

php

laravel-4

I created a migration script that creates a table called Archives.

It has 4 columns not including timestamps/increments. The tables columns are name, title, content and image.

I need to drop the image column as it is no longer needed.

I have not been able to find a way to do this by searching and was hoping someone could give me the best way to do this.

I attempted to accomplish this by creating a new migration and then running

php artisan migrate:rollback --pretend

But this tries to roll back the previous migration.

class DropImageColumnArchive extends Migration
{

    /**
     * Run the migrations.
     * @return void
     */
    public function up()
    {
        Schema::table('archives', function ($table) {
            $table->text('image');
        });
    }

    /**
     * Reverse the migrations.
     * @return void
     */
    public function down()
    {
        Schema::table('archives', function ($table) {
            $table->drop_column('image');
        });
    }
}
like image 868
CharliePrynn Avatar asked Jun 03 '14 15:06

CharliePrynn


People also ask

How do I remove a column from a database in migration?

Create the Migration To remove the columns you have to run the make migration command but instruct it as removing instead of creating a new migration. Make use of the "--table=your-table-name" flag and specify the table that you want to remove the columns.

How do I add a column to an existing table in laravel migration?

The Laravel migrations will use the Schema facade to create and modify database tables and columns: Schema::create('tasks', function (Blueprint $table) { $table->bigIncrements('id'); $table->timestamps(); }); Inside the facade, you could specify the different columns that you want to add.


1 Answers

Almost there change drop_column to dropColumn

Referenced Here http://laravel.com/docs/schema#renaming-columns

Schema::table('users', function($table)
{
    $table->dropColumn('votes');
});

Edit Referenced here for 5.1 http://laravel.com/docs/5.1/migrations

like image 178
Jonny C Avatar answered Sep 21 '22 09:09

Jonny C