Laravel Add a New Column to Existing Table in a Migration

Laravel Add a new column to existing table in a migration

To create a migration, you may use the migrate:make command on the Artisan CLI. Use a specific name to avoid clashing with existing models

for Laravel 5+:

php artisan make:migration add_paid_to_users_table --table=users

for Laravel 3:

php artisan migrate:make add_paid_to_users

You then need to use the Schema::table() method (as you're accessing an existing table, not creating a new one). And you can add a column like this:

public function up()
{
Schema::table('users', function($table) {
$table->integer('paid');
});
}

and don't forget to add the rollback option:

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

Then you can run your migrations:

php artisan migrate

This is all well covered in the documentation for both Laravel 4 / Laravel 5:

  • Schema Builder
  • Migrations

And for Laravel 3:

  • Schema Builder
  • Migrations

Edit:

use $table->integer('paid')->after('whichever_column'); to add this field after specific column.

Add new columns to existing table in a migration in Laravel

If you check at the error trace:

Base table or view already exists: 1050 Table 'users' already exists

This means that the users table already exists so when you run your migrations it is trying to create a table that is already created in your database.

Note: Don't forget to backup your database first

Delete users table from the database also delete users entries from migrations table.

After, execute the migrate Artisan command:php artisan migrate


Now another your Question is: How to add new columns in my existing table?

You have to create a table using this command:

php artisan make:migration create_users_table

The output you got it like this: Created Migration: 2019_04_12_070152_create_users_table

Your Migration structure is something this:

public function up()
{
Schema::create('users', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->string('email')->unique();
$table->string('password');
$table->rememberToken();
$table->timestamps();
});
}

Now you want to add new columns in your existing users table

php artisan make:migration add_phone_number_to_users_table --table=users

use the Schema::table() method (as you're accessing an existing table, not creating a new one). And you can add a column like this:

public function up()
{
Schema::table('users', function (Blueprint $table) {
$table->string('phonenumber')->after('name'); // use this for field after specific column.
});
}

/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::table('users', function (Blueprint $table) {
$table->dropColumn('phonenumber');
});
}

After, you can run your migrations: php artisan migrate

Your new columns(phonenumber) are now added to your existing users table, which you can view in your database.

If you have still any doubt, see this video

Adding column in existing table without using migration in Laravel

Migration files are only an easy way to create tables and other db actions, you still can use raw queries on Laravel anywhere by using DB::raw(...); check its documentation (https://laravel.com/docs/5.8/queries) to proper use it.

You may also be able to write a Schema::table(,,,) anywhere on your code, it will return some a Schema object which you can execute some method to run the code on your apo runtime, if you are using some IDE just try to explore and see what will you have by typing Schema::table(...)->

Laravel: Add column to table with updated migration

It sounds like you are trying to add a column to a table that has already been created via migration. If that is the case, rather than using Schema::create(...), you need to use Schema::table(...).

Typically, you would create a new migration for this:

$ php artisan make:migration add_img_url_to_categories

Which will create a new file at /database/migrations called something like 2019_10_21_165554_add_img_url_to_categories.php. Then add this code to the up() function:

public function up()
{
Schema::table('categories', function (Blueprint $table) {
$table->string('img_url');
});
}

Another option you have is to edit the migration exactly as you have done (per the code in your question), and then run:

$ php artisan migrate:fresh // drop all tables and re-run all migrations

or

$ php artisan migrate:refresh // reset and re-run all migrations

But keep in mind that these are both destructive operations – meaning you will lose any data you already have in your database. In early development, that might not matter. But you should really establish the habit of creating new migrations for database changes, rather than editing existing migrations.

The purpose of migrations is so that you (or anyone using your app) can quickly deploy a database that matches the schema your app is expecting.
During development, it is not uncommon to edit your migrations as you tweak your database schema to accommodate the features you are developing.

However, once you have deployed or published your app, you should consider all of the migrations to be locked or read-only. Any database changes from that point should be done in a new migration.

How to add extra columns to an existing migration table without lossing data in laravel?

You create a new migration php artisan make:migration add_extra_fields_passport_table, the migration should look like this.

public function up(): void
{
Schema::table('passport_table', function (Blueprint $table) {
$table->string('country')->after('image');
$table->string('station')->after('country');
});
}

public function down(): void
{
Schema::table('passport_table', function (Blueprint $table) {
$table->dropColumn('country');
$table->dropColumn('station');
});
}

You use the migrations, to secure all databases are in sync, local, staging, production etc. If you rewrite migrations, they will not be in sync if some migrations has been executed. I use the after() method, to secure a proper placement in the table after the image column.

If you are only developing locally alone and nothing has been released you can change the original migration. Then you have to delete the migration row that corresponds to your file in the migrations table and clean up your table structure to avoid errors with rerunning you migration. Alternatively migrations can be rolled back, that is another aproach. Then you can run php artisan migrate and it will rerun the same code again. But it is recommended to just create a new migration, it is way easier.



Related Topics



Leave a reply



Submit