Laravel Migration Add Column After Column Example
Hello,
In this example, you will learn laravel migration add column after column. I would like to share with you laravel migration add column after. This post will give you a simple example of how to add column after specific column in laravel migration. This tutorial will give you a simple example of laravel migration add column after specific column. Alright, let’s dive into the details.
In this example, I created a "posts" table and I want to add a new column status after the body column. I will use after() to add a column after a specific column in the table using laravel migration. you can easily set with laravel 6, laravel 7, laravel 8, laravel 9, laravel 10 and laravel 11 version.
so let's see bellow simple examples:
Create Migration Command:
php artisan make:migration add_new_columns_posts
Example 1: using after()
database/migrations/2023_01_16_134448_add_new_columns_posts.php
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class CreateItemsTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::table('posts', function($table)
{
$table->tinyInteger('status')->after('body');
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
}
}
Now, you can run migration:
php artisan migrate
Output:
i hope it can help you...
Hardik Savani
I'm a full-stack developer, entrepreneur and owner of ItSolutionstuff.com. I live in India and I love to write tutorials and tips that can help to other artisan. I am a big fan of PHP, Laravel, Angular, Vue, Node, Javascript, JQuery, Codeigniter and Bootstrap from the early stage. I believe in Hardworking and Consistency.
We are Recommending you
- How to Create Migration in Laravel 9?
- How to Run Migration and Seeder on Laravel Vapor?
- How to Change Column Length using Laravel Migration?
- How to Update Enum Value in Laravel Migration?
- Laravel Migration Enum Default Value Example
- Laravel Migration Add Enum Column Example
- How to Rollback Migration in Laravel?
- Laravel Migration Add Comment to Column Example
- How to add Default Value of Column in Laravel Migration?
- Laravel Migration Custom Index Name Example
- How to Add Foreign Key in Laravel Migration?
- How to Change Table Name using Laravel Migration?
- How to Remove Column from Table in Laravel Migration?
- How to Change Column Name and Data Type in Laravel Migration?