问题描述
我想覆盖在Blueprint
类中找到的timestamps()
函数.我该怎么办?
I want to override the timestamps()
function found in the Blueprint
class. How can I do that?
例如
public function up() {
Schema::create('users', function (Blueprint $table) {
$table->increments('id');
$table->string('username')->unique();
$table->string('password');
$table->string('email');
$table->string('name');
$table->timestamps(); // <-- I want this to call my method instead of the one found in the base Blueprint class
});
}
推荐答案
有一个新的blueprintResolver
函数,该函数带有一个回调函数,然后该回调函数返回Blueprint
实例.
There is a new blueprintResolver
function which takes a callback function which then returns the Blueprint
instance.
因此,像这样创建您的自定义Blueprint类:
So create your custom Blueprint class like this:
class CustomBlueprint extends Illuminate\Database\Schema\Blueprint{
public function timestamps() {
//Your custom timestamp code. Any output is not shown on the console so don't expect anything
}
}
,然后在返回CustomBlueprint
实例的地方调用blueprintResolver
函数.
And then call the blueprintResolver
function where you return your CustomBlueprint
instance.
public function up()
{
$schema = DB::connection()->getSchemaBuilder();
$schema->blueprintResolver(function($table, $callback) {
return new CustomBlueprint($table, $callback);
});
$schema->create('users', function($table) {
//Call your custom functions
});
}
我不确定使用DB::connection()->getSchemaBuilder();
创建新的架构实例是否是最新技术,但是否可行.
I'm not sure if creating a new schema instance with DB::connection()->getSchemaBuilder();
is state of the art but it works.
您还可以覆盖Schema
外观,并默认添加自定义蓝图.
You could additionally override the Schema
facade and add the custom blueprint by default.
这篇关于扩展蓝图课程?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!