我正在使用 Laravel 5.7 和 SQL Server 2017,我想生成一个varchar(50)名为name.
执行此代码给了我一个nvarchar(50)代替:
Schema::create('test', function(Blueprint $table) {
$table->string('name', 50);
});
如何区分创建varchar或nvarchar字段?
我正在使用 Laravel 5.7 和 SQL Server 2017,我想生成一个varchar(50)名为name.
执行此代码给了我一个nvarchar(50)代替:
Schema::create('test', function(Blueprint $table) {
$table->string('name', 50);
});
如何区分创建varchar或nvarchar字段?
这是在黑暗中拍摄的,因为我没有要测试的 SQL Server。但基本上你可以扩展Blueprint和SqlServerGrammar类并添加你自己的列类型。请测试并告诉我。:)
Schemas在文件夹下创建一个文件夹,app然后在文件夹下创建文件夹。在它们里面,创建你的 PHP 类:BlueprintsGrammarsSchemas
自定义蓝图.php
<?php
namespace App\Schemas\Blueprints;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Schema\Builder;
class CustomBlueprint extends Blueprint
{
public function varChar($column, $length = null)
{
$length = $length ? : Builder::$defaultStringLength;
return $this->addColumn('varChar', $column, compact('length'));
}
}
自定义语法.php
<?php
namespace App\Schemas\Grammars;
use Illuminate\Database\Schema\Grammars\SqlServerGrammar;
use Illuminate\Support\Fluent;
class CustomGrammar extends SqlServerGrammar
{
protected function typeVarChar(Fluent $column)
{
return "varchar({$column->length})";
}
}
您的迁移文件:
public function up()
{
DB::connection()->setSchemaGrammar(new CustomGrammar());
$schema = DB::connection()->getSchemaBuilder();
$schema->blueprintResolver(function($table, $callback) {
return new CustomBlueprint($table, $callback);
});
$schema->create('test', function (CustomBlueprint $table) {
$table->string('name', 50); // <-- nvarchar(50)
// or
$table->varChar('name', 50); // <-- varchar(50)
});
}