- Forums
- laravel
- Laravel Migrations: Add New Column To Existing Table (very Easy Simple)
Follow these instructions on how you add a new column to an existing table in your database with laravel migrations [5282], Last Updated: Mon Jun 24, 2024
edw
Mon Jan 15, 2024
0 Comments
230 Visits
Add a new column to table:
For example, lets add a new column called shipping_price to the 'orders' table, use this command:
$ php artisan make:migration add_shipping_column_to_orders_table
A new filed called 202x_xx_1xx_00xxx_add_shipping_column_to_orders_table.php will be created
To add a new shipping_price edit the file with the following up and down methods example:
return new class extends Migration
{
/**
* Run the migrations.
*/
public function up(): void
{
Schema::table('orders', function (Blueprint $table) {
$table->decimal('shipping_price', 20, 2);
});
}
/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::table('orders', function (Blueprint $table) {
$table->dropColumn('shipping_price');
});
}
};
Run the migrations command to add the column in the database:
$ php artisan migrate
Now the new column will be added:
Additional Notes form the useful video below:
2jQ2M5vD5sM
php artisan make:migration create_blogs_table --create=blogs
xxx_create_blogs_table.php
schema::create('blogs')
php artisan make:migration add_author_to_blogs_table
xxxx add_author_to_blogs_table.php
php artisan migration:rollback