If you're using Laravel just use migrations... That'd be the cleanest way to do mass DB updates. Then update the records using their models, and use Carbon to handle the dates:
<?php
use App\TableModel;
use Carbon\Carbon;
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class UpdateColumnDatatype extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
// Get all the data to reformat it
$data = TableModel::all();
Schema::table('table_name', function (Blueprint $table) {
$table->dropColumn('column_name'); // Removes the old column
// You need to set a ->default() or make this column ->nullable() or you'll
// get errors since the data is empty now.
$table->dateTime('column_name'); // Creates the new datetime
});
// Iterate over the old data to update the new rows
foreach($data as $model) {
$oldDate = Carbon::createFromFormat('d/m/Y h:i', $model->column_name);
$newDate = $oldDate->format('Y-m-d H:i:s'); // Format for DateTime
$obj = TableModel::find($model->id);
$obj->column_name = $newDate; // Change the record's data
$obj->save(); // Save the changes
}
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::table('table_name', function (Blueprint $table) {
$table->dropColumn('column_name'); // Removes the new column
$table->string('column_name'); // Creates the old column as a string (varchar equivalent)
// Do the same thing to turn the new datetime values back to your old format
// in case you decide to do a rollback
});
}
}