How to add columns in existing table using migration in laravel
How to add columns in existing table using migration in laravel
Through many examples, we will learn how to resolve the "How to add columns in existing table using migration in laravel".
-
Add new columns in existing table using migration in laravel 8
//1. Run php artisan make:migration command to create migration file. php artisan make:migration add_publish_to_comments --table=comments //2. Add columns to your migration file under - database\Migrations\<your_migration_filename>.php. public function up() { Schema::table('comments', function (Blueprint $table) { $table->boolean('is_publish')->default(0); }); } public function down() { Schema::table('comments', function (Blueprint $table) { $table->dropColumn('is_publish'); }); } //3. Migration command to create database table structure.</p> //Migrate to this file php artisan migrate --path=database\Migrations\2021_04_12_085751_add_publish_to_comments.php Or //Migrate all files php artisan migrate
You can add columns in existing table using the migration command
php artisan make:migration add_publish_to_comments --table=comments
. Use a specific migration file name which is not already exist in your migrations folder. After that add columns to the migration file and run migrate command. This is how you can add columns to your existing database table in laravel without changing the table structure manually using PhpMyAdmin MySQL Database. Change table name, migration file name as per your requirements.
If you like what you are reading, please consider buying us a coffee ( or 2 ) as a token of appreciation.
Don't forget to share this article! Help us spread the word by clicking the share button below.
We appreciate your support and are committed to providing you valuable and informative content.
We are thankful for your never ending support.
Random Code Snippet Queries: Laravel
- How to make Copy or Duplicate table row in laravel
- How to use bootstrap pagination in laravel 8
- How to remove P tag from CkEditor in Laravel?
- Laravel migration add foreign key to existing table
- Extra Filter Query on Relationships in Laravel
- RuntimeException You must enable the openssl extension in your php.ini to load information from https://repo.packagist.org
- Route prefix with auth middleware in laravel
- In order to use the Auth::routes() method, please install the laravel/ui package
- How to display a specific word from a string in laravel
- Return view from route Laravel
- The use statement with non-compound name 'DB' has no effect
- Generate unique username in Laravel
- How to get route method name in Laravel
- How to disable timestamps in laravel
- Laravel delete all rows older than 30 days
- Update if exist else insert new record in laravel
- How to get last record from object collection in laravel
- SQLSTATE[42000]: Syntax error or access violation: 1075 Incorrect table definition; there can be only one auto column and it must be defined as a key
- Attempt to read property "avatar" on null in Laravel
- How to upload local Laravel project to server ?
- If condition in Laravel 9
- Laravel file size validation not working
- Call to undefined function Illuminate\Encryption\openssl_cipher_iv_length()
- Delete all related comments on deleting a post in Laravel
- How to add foreign key in laravel using migration