Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Updating table scheme without affecting data in Laravel

I am new to Laravel from code igniter and I am LOVING THE FRAMEWORK! My life is so much easier now.

I have created a table with columns using php artisan and entered some test data. I now want to add a few new columns to the database without affecting the current data, and setting the new fields to be null.

My inital thought was to enter a new field in the database migrate file and the run "php artisan migrate", but this just gave me the message "nothing to migrate" and did enter the new column in my database.

Here is my database migrate file:

<?php

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

class CreateFestivalsTable extends Migration {

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

}

public function down()
{
    Schema::drop('festivals');
}

}
like image 351
dallardtech Avatar asked Dec 11 '13 10:12

dallardtech


People also ask

How do I move a table in Laravel without losing data?

Set your column details there, run the migrations using php artisan migrate and that's all. You'll have this new column in your users table without losing previously stored data.

How do you add multiple new columns in Laravel migration without losing data?

As per the docs, you just need to create a separate migration to create the new column. This will allow you to add a column without resetting or rolling back your tables, and thus prevent you from losing your data.

What is difference between migrate fresh and refresh in Laravel?

php artisan migrate:fresh is used when we want a fresh or new installation of our database. It deletes all the existing tables of the database and runs the migrate command. php artisan migrate:refresh is a two in one command that executes the :rollback command and the migrate command.


1 Answers

create new migration with artisan name it addColumnFestivalTable

<?php

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

class addColumnFestivalTable extends Migration {

public function up()
{
    Schema::table('festivals', function($table)
    {
        $table->string('new_col_name');
     });

}

public function down()
{
    Schema::table('festivals', function($table)
    {
       $table->dropColumn('new_col_name');
    });
}

}

for more information read Laravel 5.4 doc

like image 106
umefarooq Avatar answered Oct 28 '22 05:10

umefarooq