Update tables using Laravel migrations

Submitted by selenearzola - 3 years ago

Sometimes you need to add a new column to a table that already exists, for this we can use laravel migrations. In our command line we type (You can name the migration file as you want ) : php artisan migrate: make add_field_to_table. In line 14 we're adding a new field (table column) into our table. We type the field type if you don't know the field types you can find them in the documentation: http://laravel.com/docs/4.2/schema. Continuing with, we can specified if we want to add the field before or after an a column that already exists of course. In this case i'm using after but you should use whatever you need. Once we have finish, we go back to the command line we type: php artisan migrate. And that's it, we've updated our table. I guess that we can add as many fields as we need, i didn't try that but i think that it will works too. Cheers!

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

class add_field_to_table extends Migration {

    /**
	 * Run the migrations.
	 *
	 * @return void
	 */
	public function up()
	{
		Schema::table('tableName',function(Blueprint $table){         
            $table->text('newField')->after('fieldName');/*Line 14*/
		});
		
	}

	/**
	 * Reverse the migrations.
	 *
	 * @return void
	 */
	public function down()
	{
		//
	}

Ads cover the costs of maintaining Laravel-tricks

comments powered by Disqus

Ads cover the costs of maintaining Laravel-tricks