One of the powerful features of Laravel is the powerful database migration processes. It is easy to add, or delete, columns from existing tables and the artisan command does much of the boilerplate work for you.
Within an existing Laravel directory, you can create a migration by issuing the
php artisan make:migration add_notes_to_my_table
command. This will create a migration file with a timestamp in the database/migrations
directory such as 2019_09_13_090000_add_notes_to_my_table.php
so it is superior to provide artisan with the table name as a command line option
php artisan make:migration add_notes_to_my_table --table=mytable
which creates a boilplate file with looks like this
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class AddNotesToMyTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::table('mytable', function (Blueprint $table) {
//
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::table('mytable', function (Blueprint $table) {
//
});
}
}
You can see that the class name is the name you provided artisan in camel case, and that the up()
and down()
functions are pre-populated with the table name. All that remains is to add the field(s) you wish to add to the table thusly;
public function up()
{
Schema::table('mytable', function (Blueprint $table) {
$table->text('notes);
});
}
but remember to also update the down()
function so the migration can be rolled-back.
public function down()
{
Schema::table('mytable', function (Blueprint $table) {
$table->text('notes');
});
}
All that remains is to run you migration
php artisan migrate
Note that there are some modifiers available, such as ->after('column')
but be careful as most of them only apply to certain database products.