How7o
  • Home
  • Tools
  • Prank Screens
  • Learn
  • Blog
  • Contact
Reading: How to Change a MySQL Column Type in Laravel Migration
Share
How7oHow7o
Font ResizerAa
  • OS
Search
  • Home
  • Tools
  • Prank Screens
  • Learn
  • Blog
  • Contact
Follow US
© 2024–2026 How7o. All rights reserved.
How7o > Free Laravel, PHP, WordPress & Server Tutorials > Web Development > How to Change a MySQL Column Type in Laravel Migration
Web Development

How to Change a MySQL Column Type in Laravel Migration

how7o
By how7o
Last updated: April 19, 2026
6 Min Read
Laravel migration converting a MySQL column type from VARCHAR to DECIMAL without losing data
SHARE

Changing a column’s data type on a table that already has data is one of those migrations that feels risky until you’ve done it a few times. You can’t migrate:refresh — that wipes everything — so you need an ALTER TABLE that preserves the existing rows. Laravel’s schema builder has a change() method that handles exactly this, and in Laravel 10 and later it doesn’t require any extra packages.

Contents
  • TL;DR
  • Step 1: Generate the Migration
  • Step 2: Use change() in the Migration
  • Step 3: Write a Reversible down() Method
  • Step 4: Run the Migration
  • Note on doctrine/dbal
  • Preserving All Column Modifiers
  • Troubleshooting
    • Data Truncated for Column
    • Change Silently Dropped a Default/Nullable
  • Frequently Asked Questions
  • Related Guides

Originally published February 17, 2023, rewritten and updated April 17, 2026.

TL;DR

Create a migration with php artisan make:migration alter_{table}_change_{column} --table={table}, then call $table->decimal('column_name', 8, 2)->change(); in the up() method and php artisan migrate. Data in the column is converted in place.

Step 1: Generate the Migration

Give the migration a name that describes what’s changing so it’s obvious later:

php artisan make:migration alter_transactions_change_amount_to_decimal --table=transactions

The --table= flag scaffolds Schema::table() (alter existing) instead of Schema::create().

Step 2: Use change() in the Migration

Open the generated file and fill in up() with the new column definition, chaining ->change() at the end:

public function up(): void
{
    Schema::table('transactions', function (Blueprint $table) {
        $table->decimal('amount', 8, 2)->change();
    });
}

The existing data is converted in place. Values that fit the new type (e.g. numeric strings going to decimal) are preserved; values that don’t fit may be truncated or set to zero depending on MySQL’s strict-mode settings, so always take a database backup before running this in production.

Step 3: Write a Reversible down() Method

The down() method should restore the column to its original type so rollbacks work:

public function down(): void
{
    Schema::table('transactions', function (Blueprint $table) {
        $table->string('amount')->change();
    });
}

Note that converting from DECIMAL back to VARCHAR is safe (any decimal fits in a string), but the opposite direction is not always safe — which is why the backup matters.

Laravel migration using change() to alter a column type from VARCHAR to DECIMAL on an existing table

Step 4: Run the Migration

php artisan migrate

After the migration completes, verify the new type with a quick describe transactions in your database client, and spot-check a few rows to confirm the data looks right.

Note on doctrine/dbal

Older Laravel guides (before Laravel 10) instruct you to install doctrine/dbal for change() to work:

composer require doctrine/dbal

If you’re on Laravel 9 or earlier, this is still required. On Laravel 10+, the schema builder can alter columns natively and doctrine/dbal is no longer needed — you can remove it from composer.json if it’s only there for migrations.

Preserving All Column Modifiers

When you call change(), Laravel rewrites the column definition completely. That means all modifiers need to be listed in the migration — otherwise they get dropped. For example, if the column was originally nullable and had a default value, you need to declare them again:

$table->decimal('amount', 8, 2)->nullable()->default(0)->change();

Check the original migration (or describe {table}) before running change() so you can carry the modifiers forward.

Troubleshooting

Data Truncated for Column

MySQL emits this when the existing data doesn’t fit the new type (e.g. converting VARCHAR to INT with non-numeric values). Audit the data with a SELECT before the migration, clean up bad rows, and re-run.

Change Silently Dropped a Default/Nullable

This is almost always because the change() call didn’t re-declare the modifier. Add ->nullable(), ->default(...), ->unsigned() etc. to the new declaration.

Frequently Asked Questions

How do I change a column’s data type in Laravel without losing data?

Create a migration with <code>–table={table}</code>, call the new type definition chained with <code>->change()</code>, then run <code>php artisan migrate</code>. Laravel issues an ALTER TABLE that converts the data in place — no refresh required.

Do I still need doctrine/dbal to change columns in Laravel?

Only on Laravel 9 and earlier. Laravel 10 added native support for altering columns to the schema builder, so <code>doctrine/dbal</code> is no longer a requirement on Laravel 10+ projects.

Why does change() drop my nullable or default modifier?

<code>change()</code> rewrites the whole column definition. Any modifier not listed in the new call is removed. Always re-declare <code>->nullable()</code>, <code>->default(…)</code>, <code>->unsigned()</code> etc. when using <code>change()</code>.

Is it safe to change a column type in production?

Take a database backup first, and run the migration in a maintenance window if the table is large — an ALTER TABLE on a multi-million-row table can lock the table for minutes. For high-traffic tables, consider online schema-change tools like gh-ost or pt-online-schema-change.

Related Guides

  • How to Add New Columns to an Existing Table in Laravel Migration
  • How to Add Foreign Keys in Laravel Migration
  • How to Install MySQL on Ubuntu

For the full list of column modifiers, see the official Laravel migrations documentation.

TAGGED:EloquentLaravelmysqlphp

Sign Up For Daily Newsletter

Be keep up! Get the latest breaking news delivered straight to your inbox.
[mc4wp_form]
By signing up, you agree to our Terms of Use and acknowledge the data practices in our Privacy Policy. You may unsubscribe at any time.
Share This Article
Facebook Copy Link Print
Previous Article Laravel migration adding two new columns to an existing transactions table How to Add New Columns to an Existing Table in Laravel Migration
Next Article Deleting a Laravel user cascades to remove related posts, photos, and notifications How to Delete Related Records in Laravel Eloquent
Leave a Comment

Leave a Reply Cancel reply

You must be logged in to post a comment.

FacebookLike
XFollow
PinterestPin
InstagramFollow
Most Popular
Bun runtime — faster JS toolkit replacing npm in Laravel projects
How to Install Bun Runtime on Ubuntu (And Use It in a Laravel Project)
May 24, 2026
Tailscale mesh — peer-to-peer connections between devices, coordination server
How to Install Tailscale on Ubuntu (Zero-Config Mesh VPN for Self-Hosters)
May 24, 2026
Caddy server — automatic HTTPS, 3-line Caddyfile vs 25-line nginx config
How to Install Caddy Server on Ubuntu (Automatic HTTPS, Drop-in nginx Alternative)
May 24, 2026
Cloudflare Tunnel — outbound-only connection from server, no inbound port forward
How to Install Cloudflare Tunnel on Ubuntu (Expose Local Services, No Port Forwarding)
May 24, 2026
WireGuard encrypted tunnel between server and clients with lock icons
How to Set Up WireGuard VPN on Ubuntu (Server, Linux Client, and iOS)
May 24, 2026

You Might Also Like

Install Node.js on Ubuntu — terminal with NodeSource setup_22.x curl command and Node.js hexagon icon
Web Development

How to Install Node.js on Ubuntu (22.04 & 24.04): Step-by-Step

11 Min Read
MySQL connect remote from Ubuntu — mysql-client + mysql -h host
Server Management

How to Connect to a Remote MySQL Database from Ubuntu

7 Min Read
MySQL extract digits from string — REGEXP_REPLACE negation class
Web Development

How to Extract Only the Digits from a String in MySQL

6 Min Read
WooCommerce dynamic currency switcher — cookie-stored currency applied via woocommerce_currency filter
Web Development

How to Dynamically Change Currency in WooCommerce

7 Min Read
How7o

We provide tips, tricks, and advice for improving websites and doing better search.

Tools

  • Age Calculator
  • Word Counter
  • Image Upscaler
  • Password Generator
  • QR Code Generator
  • See all tools→

Pranks

  • Fake Blue Screen Prank
  • Hacker Typer
  • Fake iMessage Generator
  • Windows XP Crash Prank
  • Windows 11 Update Prank
  • See all prank screens →

Company

  • About Us
  • Blog
  • Contact
  • Privacy Policy
  • Terms of Service
  • Sitemap
© 2024–2026 How7o. All rights reserved.
Welcome Back!

Sign in to your account

Username or Email Address
Password

Lost your password?