Pao*_*der 3 mysql laravel laravel-5.5
使用Laravel 5.5和Mysql(10.1.19-MariaDB)
对于md5哈希,我想要一个binary(16)列。我们称该列为url_hash
使用时:
$table->binary('url_hash');
Run Code Online (Sandbox Code Playgroud)
它会给我一个BLOB列。
来源:https : //laravel.com/docs/5.5/migrations#creating-columns
我已经在网络上看到过各种各样的黑客或插件,但是没有任何可能在下一次更新时中断的外部插件的最简单的工具是什么?
干杯
扩展MySqlGrammar类,例如app/MySqlGrammar.php:
namespace App;
use Illuminate\Support\Fluent;
class MySqlGrammar extends \Illuminate\Database\Schema\Grammars\MySqlGrammar {
protected function typeRealBinary(Fluent $column) {
return "binary({$column->length})";
}
}
Run Code Online (Sandbox Code Playgroud)
然后使用宏添加您自己的列类型:
DB::connection()->setSchemaGrammar(new \App\MySqlGrammar());
Blueprint::macro('realBinary', function($column, $length) {
return $this->addColumn('realBinary', $column, compact('length'));
});
Schema::create('table', function(Blueprint $table) {
$table->realBinary('url_hash', 16);
});
Run Code Online (Sandbox Code Playgroud)
您可以将字符集设置为binary。
$table->char('url_hash', 16)->charset('binary');
Run Code Online (Sandbox Code Playgroud)
这实际上在 MySQL Workbench 中显示为长度为 16的真实二进制列类型。
应该没有任何区别:https : //stackoverflow.com/a/15335682/5412658
Laravel 作者建议进行DB:statement调用并运行原始 SQL。
如果您正在运行迁移,则可以在以下之后运行此原始 SQL Schema::create:
DB::statement('ALTER TABLE table_name ADD url_hash binary(16) AFTER some_column');
Run Code Online (Sandbox Code Playgroud)
根据用例,您可能需要在删除表之前运行此原始 SQL 来删除列:
DB::statement('ALTER TABLE table_name DROP url_hash');
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1289 次 |
| 最近记录: |