【发布时间】:2013-07-26 06:34:42
【问题描述】:
谁能详细地告诉我如何扩展 Illuminate\Database\Schema 以添加一个 zerofill 函数并修改当前整数函数以获取一个长度参数,而实际上并没有修改任何供应商文件。
谢谢。
编辑:或者有没有办法使用 Schema Builder 手动添加一列,我只是错过了周五下午头重脚轻的原因?
编辑:采用 Gareth Oakley 建议的方法,在 Schema::create() 之后添加 DB::statement() 调用:
<?php
class CreateExamplesTable extends Migration {
public function up()
{
Schema::create('examples', function(Blueprint $table)
{
$table->engine = 'InnoDB';
$table->increments('id')->unsigned();
$table->integer('account_id')->unsigned();
$table->string('card_id', 20)->nullable();
$table->boolean('active')->default(true);
$table->timestamps();
$table->foreign('account_id')->references('id')->on('accounts')->onDelete('cascade')->onUpdate('cascade');
});
DB::statement('ALTER TABLE examples ADD invoice_id INT(6) UNSIGNED ZEROFILL NOT NULL AFTER card_id');
}
public function down()
{
Schema::drop('examples');
}
}
【问题讨论】:
-
Laravel 不提供这样的功能。您可以选择一种 smallinteger/integer/biginteger 类型 - 选择最适合的一种。就个人而言,我不会在数据库端使用 zerofill。我会在应用程序端格式化输出。
-
我在下面发布了一个答案 - 但同意了。我通常会留下与数据如何在数据库之外呈现相关的任何内容,并将其放在我的视图中。你可以使用
str_pad- php.net/manual/en/function.str-pad.php