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
- Call to a member function update() on null
- SQLSTATE[42S02]: Base table or view not found: 1146 Table 'laravel8.projects' doesn't exist
- Redirect to another view from controller in laravel
- How to insert ckeditor data into database in Laravel?
- Generate random string lowercase in Laravel
- Insert dummy data in users table Laravel
- How to get session in blade Laravel ?
- How to pass query string to url in laravel
- Undefined property: stdClass::$title
- Import/Use Storage facade in laravel
- Get the post details if it has at least one comment in comments table
- How to increment column value of table in Laravel
- Target class [App\Http\Controllers\Auth\Request] does not exist.
- Laravel hasmany select not working
- Run artisan command to generate key in laravel
- Call to a member function getRelationExistenceQuery() on array in Laravel
- How to access the nth object from Laravel collection object ?
- Update existing pivot table data in laravel
- Laravel 10 Breeze Authentication Example
- How to check email is valid or not in Laravel
- First and last item of the array using foreach iteration in laravel blade
- How to get specific columns using with method in laravel Eloquent relationship
- How to check query string exists or not in laravel blade
- Send post data from controller to view
- Symlink(): No such file or directory