Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

edgreenberg's avatar

Alter table in raw sql

I have a table with mixed case column names. When I try to alter a column, migration calls Doctrine\Dbal and it can't find the lower case version of the column. I've looked high and low for a way around this. No luck.

So I want to alter my table/column myself:

DB::table('user')->select(DB::raw('alter table `user` alter column `TCAccess` set default 0'));

The above doesn't work, and with an enabled query log, the DB::getQueryLog() returns an empty array.

Is there a way to execute the sql shown inside the DB::raw call above?

Thanks,

Ed G

0 likes
2 replies
MNSY's avatar

Hi, you can use this instead:

DB::statement('ALTER TABLE users ADD mobile VARCHAR(13) );

4 likes
readerstacks's avatar

You can use as below

<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class CreateTestTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
      DB::statement('ALTER TABLE `users` MODIFY `age` DATETIME');         
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('users'); \or whatever you want on rollback
    }
}

Also i have create a package to manage raw sql query migration (Queries created using phpMyAdmin or tool but manage the migration across the developer)

Check here : https://readerstacks.com/how-to-run-raw-sql-query-in-migration-laravel/

Please or to participate in this conversation.