How to Add a New Column in Laravel
Adding a new column to an existing table in Laravel is something we do constantly as product requirements jump around. Running an SQL script directly against your database might be faster in the moment, but skipping migrations always ends up causing environment drift and deployment headaches.
Let's say we need to add a paid column to our users table.
Here's the standard way to do it:
-
Generate the Migration: From your project root, run:
php artisan make:migration add_paid_to_users_table --table=usersPassing
--table=userstells Artisan to spit out the boilerplate for modifying an existing table rather than creating a new one. -
Define the Schema Change: Open the generated file in
database/migrations. Update theupmethod with your new column:use Illuminate\Database\Migrations\Migration; use Illuminate\Database\Schema\Blueprint; use Illuminate\Support\Facades\Schema; public function up() { Schema::table('users', function (Blueprint $table) { $table->integer('paid')->default(0); // Or boolean('paid')->default(false) }); }Always remember to define the
downmethod so you can easily roll back if things go wrong:public function down() { Schema::table('users', function (Blueprint $table) { $table->dropColumn('paid'); }); } -
Run the Migration: Apply the change:
php artisan migrate
Pro Tip:
If it bothers you to append new columns at the very end of the table, you can slot them exactly where you want them using ->after(). It doesn't actually affect database performance, but the table reads cleaner:
$table->integer('paid')->default(0)->after('email');