i know how to add column/field to to database for example
<?php
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class AddStatusToPhoto extends Migration {
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::table('photos', function(Blueprint $table)
{
$table->integer('status');
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
//
}
}
but it will append to the last field of the table, but i want to add to specific column e.g insert the field after created_at, is it possible to do that?
The Laravel migrations will use the Schema facade to create and modify database tables and columns: Schema::create('tasks', function (Blueprint $table) { $table->bigIncrements('id'); $table->timestamps(); }); Inside the facade, you could specify the different columns that you want to add.
database\migration\add_new_column_to_products_table.php Now you can run migrate command to add this new field. Simply you can run below command to auto add a new field. Hope it can help you. Laravel Migration example tutorial, in this tutorial you have learned how to add new column to a table without losing data.
As per the docs, you just need to create a separate migration to create the new column. This will allow you to add a column without resetting or rolling back your tables, and thus prevent you from losing your data.
There is a after
modifier you can use if you are on MySQL.
$table->integer('status')->after('created_at');
->after('column')
Place the column "after" another column (MySQL Only)
Laravel Docs - Migrations - Creating Columns - Column Modifiers
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With