Jonathan
Jonathan

Reputation: 558

How can I change timestamps column name when using "artisan migrate"?

I am struggling to change the timestamp column names which are generated by

php artisan migrate

command.

I have already made following change. When I use eloquent query builder, it can correctly generate the column name, but when I using the command above, it still generates "created_at", "updated_at" and "deleted_at". Can anyone help me out? Thanks a lot.

/* vendor\framework\src\Illuminate\Database\Eloquent\Model.php */

/**
 * The name of the "created at" column.
 *
 * @var string
 */
const CREATED_AT = 'datetime_created';

/**
 * The name of the "updated at" column.
 *
 * @var string
 */
const UPDATED_AT = 'datetime_updated';

/**
 * The name of the "deleted at" column.
 *
 * @var string
 */
const DELETED_AT = 'datetime_deleted';

/* vendor\framework\src\Illuminate\Database\Schema\Blueprint.php */

/**
 * Indicate that the timestamp columns should be dropped.
 *
 * @return void
 */
public function dropTimestamps()
{
    $this->dropColumn('datetime_created', 'datetime_updated');
}

/**
 * Add a "deleted at" timestamp for the table.
 *
 * @return void
 */
public function softDeletes()
{
    $this->timestamp('datetime_deleted')->nullable();
}
/**
 * Add creation and update timestamps to the table.
 *
 * @return void
 */
public function timestamps()
{
    $this->timestamp('datetime_created');

    $this->timestamp('datetime_updated');
}
/**
 * Add a "deleted at" timestamp for the table.
 *
 * @return void
 */
public function softDeletes()
{
    $this->timestamp('datetime_deleted')->nullable();
}

P.S. I know it's not a good idea to modify the "core". If someone can tell me the best way to extend those classes I would really appreciate it.

Upvotes: 9

Views: 8951

Answers (1)

rmobis
rmobis

Reputation: 27002

Don't ever edit the code under the vendor folder. First, it's usually (by default) not carried with your repository, so you'd lose the changes if you or anyone else ever wanted to work on another machine. Second, it'd be overwritten at the moment you do a composer update.


Well, that being said, let's start dealing with this "modifying the core" horror. For the Illuminate\Database\Eloquent\Model.php, simply create a base model, from which you'll be extending all your subsequent models, and overwrite the constants in it:

app/models/BaseModel.php

abstract class BaseModel extends Eloquent {

    /**
     * The name of the "created at" column.
     *
     * @var string
     */
    const CREATED_AT = 'datetime_created';

    /**
     * The name of the "updated at" column.
     *
     * @var string
     */
    const UPDATED_AT = 'datetime_updated';

    /**
     * The name of the "deleted at" column.
     *
     * @var string
     */
    const DELETED_AT = 'datetime_deleted';

}

Then, for the Illuminate\Database\Schema\Blueprint case... Well, it gets bloody:

  1. Extend ..\Schema\Blueprint, overwriting the methods you mentioned.
  2. Extend ..\Schema\Builder, overwriting createBlueprint method to use your new Blueprint class.
    • Also extend ..\Schema\MySqlBuilder to extend from your new Builder class.
  3. Extend ..\Connection, overwriting getSchemaBuilder method to use your new Builder class.
    • Also extend ..\MySqlConnection, ..\PostgresConnection, ..\SqlServerConnection and ..\SQLiteConnection to extend from your new Connection class.
    • Note: ..\MySqlConnection also needs to have its getSchemaBuilder method extended to use your new MySqlBuilder class.
  4. Extend ..\ConnectionFactory, overwriting createConnection method to use your extended Connection classes.
  5. Create a ServiceProvider to register your new ConnectionFactory class as the new db.factory component, and add it on your app/config/app.php file, under providers.

So, after half an hour digging through Laravel's source code to figure that out, I came to the conclusion that it would probably be easier to simply do the following on your migrations:

$table->timestamp(BaseModel::CREATED_AT);
$table->timestamp(BaseModel::UPDATED_AT);

Upvotes: 19

Related Questions