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.
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'); });
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(); });
$table->unique('slug'); So you add unique index to existing 'slug'.
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:
..\Schema\Blueprint
, overwriting the methods you mentioned...\Schema\Builder
, overwriting createBlueprint
method to use your new Blueprint
class.
..\Schema\MySqlBuilder
to extend from your new Builder
class...\Connection
, overwriting getSchemaBuilder
method to use your new Builder
class.
..\MySqlConnection
, ..\PostgresConnection
, ..\SqlServerConnection
and ..\SQLiteConnection
to extend from your new Connection
class...\MySqlConnection
also needs to have its getSchemaBuilder
method extended to use your new MySqlBuilder
class...\ConnectionFactory
, overwriting createConnection
method to use your extended Connection
classes.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);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With