【发布时间】:2021-06-23 03:03:34
【问题描述】:
试图用 32 位二进制数创建列
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('test_binary', function (Blueprint $table) {
$table->increments('id');
$table->char('binary_number', 32)->charset('binary'); // From: https://stackoverflow.com/a/62615777/5675325
$table->timestamps();
});
}
即
$table->char('binary_number', 32)->charset('binary');
当我通过 HeidiSQL 查看它时,我可以看到它的类型为 BINARY,大小为 32。
在创建播种机以填充所需数据时,我尝试过
/**
* Run the database seeds.
*
* @return void
*/
public function run()
{
DB::table('test_binary')->insert([
'id' => 1,
'binary_number' => 2,
'created_at' => now(),
'updated_at' => now()
]);
事实证明,如果我在binary_number 中使用 2 或 101,我将得到 DB 中的结果分别为 2 或 101。
当我尝试000000000000000000000000000000000010(在 32 位二进制中等于 2)和000000000000000000000000000001100101(在 32 位二进制中等于 101)时
'binary_number' => 00000000000000000000000000000010,
然后我分别得到值 8 和 294977。
但是,我正在寻找的是将 2 存储为 0000000000000000000000000000000000000010 并将 101 存储为 000000000000000000000000000001100101。
【问题讨论】: