Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 334
Jonathan Avatar asked Jul 18 '13 23:07

Jonathan


People also ask

How do I change the column name in migration?

To rename a column, you may use the renameColumn method on the Schema builder. Before renaming a column, be sure to add the doctrine/dbal dependency to your composer. json file: Schema::table('users', function (Blueprint $table) { $table->renameColumn('from', 'to'); });

How do I change column name and data type in Laravel migration?

Following worked for me. Definitely you need to install doctrine/dbal to make this work, using following command in terminal. open your migration file and write down below. Schema::table('yourTable', function (Blueprint $table) { $table->string('column_name','4294967295')->change(); });

How do I change a column to unique in Laravel migration?

$table->unique('slug'); So you add unique index to existing 'slug'.


1 Answers

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);
like image 71
rmobis Avatar answered Oct 03 '22 02:10

rmobis