Split a `dateTime` column into a separate `date` and `time` column in Laravel 5

I have a table called 'offers' with a column called start_date of type dateTime.

I want to split this column into two separate columns called:

  • start_date of type date
  • start_time of type time

To do this I have the following code:

<?php

use App\Offer;
use Carbon\Carbon;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class FixOffersTable extends Migration
{
    public function up()
    {
        Schema::table('offers', function(Blueprint $table)
        {
            $table->renameColumn('start_date', 'start_date_time');
            $table->renameColumn('end_date', 'end_date_time');
        });

        Schema::table('offers', function (Blueprint $table)
        {
            $table->date('start_date')->after('start_date_time')->nullable();
            $table->time('start_time')->after('start_date')->nullable();

            foreach (Offer::all() as $offer) {
                /* Cannot use model mutator, as model class can change over time, and may no longer have certain columns
                in the $casts attribute. Therefore using the raw string fetched from the MySQL database. */
                $startDateTime = Carbon::createFromFormat('Y-m-d H:i:s', $offer->getOriginal('start_date_time'));
                $offer->start_date = Carbon::createFromDate($startDateTime->year, $startDateTime->month, $startDateTime->day);
                $offer->start_time = Carbon::createFromTime($startDateTime->hour, $startDateTime->minute, $startDateTime->second);
                $offer->save();
            }
        });
    }
}

However the above gives the following error:

[Doctrine\DBAL\Schema\SchemaException]                        
There is no column with name 'start_date' on table 'offers'. 

However commenting the "for loop" out means this error is no longer present, meaning the issue is somewhere there.

Better methods are also welcome!


ANSWERS:


Answered my own question. The reason I got this error is becuase of two reasons.

Reason 1, I was asking for a column to be after another column when it doesn't exist yet.

Reason 2, I was creating offers using columns that haven't been created yet.

This is how it should be done:

<?php

use App\Offer;
use App\Schedule;
use Carbon\Carbon;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class FixOffersTable extends Migration
{
    public function up()
    {
        DB::beginTransaction();

        Schema::table('offers', function(Blueprint $table)
        {
            $table->renameColumn('start_date', 'start_date_time');
            $table->renameColumn('end_date', 'end_date_time');
        });

        Schema::table('offers', function (Blueprint $table)
        {
            $table->time('start_time')->after('start_date_time')->nullable();
            $table->date('start_date')->after('start_date_time')->nullable();
        });


        foreach (Offer::all() as $offer) {
           /* Cannot use model mutator, as model class can change over time, and may no longer have certain columns
            in the $casts attribute. Therefore using the raw string fetched from the MySQL database. */
            $startDateTime = Carbon::createFromFormat('Y-m-d H:i:s', $offer->getOriginal('start_date_time'));
            $offer->start_date = Carbon::createFromDate($startDateTime->year, $startDateTime->month, $startDateTime->day);
            $offer->start_time = Carbon::createFromTime($startDateTime->hour, $startDateTime->minute, $startDateTime->second);
            $offer->save();
        }


        DB::commit();
    }
}


 MORE:


 ? Renaming column with postgreSQL in a migration
 ? Laravel migrations - foreign key not applying to the table
 ? Trying to get the username using MODELS but getting this error: Trying to get property of non-object
 ? artisan migrate command doesn't migrate, produces no output
 ? Creating MYSQL FUNCTION in Laravel 4 Migrations
 ? Php artisan migrate always exit with code 255
 ? How to point to manual install Microsoft ODBC Driver 13
 ? Error connecting to an SQL data source when using sqlsrv_connect()
 ? PHP sqlsrv_get_field returns always blank
 ? sqlsrv_connect Login Fails for User