Laravel 迁移:托管服务器上的 Json 列出错,但本地 Xampp 服务器上没有
Laravel Migration: Error in Json column on Hosting Server but not on Local Xampp Server
我最近下载并安装了 wallet 的软件包,我在其中发布了迁移,这些迁移在某些表中有 JSON
列。
迁移:
public function up(): void
{
Schema::create($this->table(), function (Blueprint $table) {
$table->bigIncrements('id');
$table->morphs('payable');
$table->enum('type', ['deposit', 'withdraw'])->index();
$table->decimal('amount', 64, 0);
$table->boolean('confirmed');
$this->json($table, 'meta')->nullable();
$table->uuid('uuid')->unique();
$table->timestamps();
$table->index(['payable_type', 'payable_id', 'type'], 'payable_type_ind');
$table->index(['payable_type', 'payable_id', 'confirmed'], 'payable_confirmed_ind');
$table->index(['payable_type', 'payable_id', 'type', 'confirmed'], 'payable_type_confirmed_ind');
});
}
public function json(Blueprint $table, string $column): ColumnDefinition
{
$conn = DB::connection();
if ($conn instanceof MySqlConnection || $conn instanceof PostgresConnection) {
$pdo = $conn->getPdo();
try {
$sql = 'SELECT JSON_EXTRACT(\'[10, 20, [30, 40]]\', \'$[1]\');';
$prepare = $pdo->prepare($sql);
$prepare->fetch();
} catch (\Throwable $throwable) {
return $table->text($column);
}
}
return $table->json($column);
}
protected function table(): string
{
return (new Transaction())->getTable();
}
public function down(): void
{
Schema::drop($this->table());
}
此迁移在本地 xampp 服务器中运行良好,但我不明白为什么我的实时服务器出现错误。以下是我在尝试 运行 迁移时遇到的错误。
我尝试使用 $table->json('meta')->nullable();
这也没有用,尽管我认为这不是一件合适的事情,因为据我所知 $this->json()
调用 json
函数.
有些人还建议使用文本而不是 Json,但我觉得这不是解决方案。我们怎样才能解决这个问题?
请检查您的 MySQL 版本是否为 5.6 或低于该功能 json 将无法使用。
您应该使用 MySQL 5.7 或更高版本才能使用 json 功能。或者在 MySQL 5.6 或更低版本中使用“文本”作为列。
我最近下载并安装了 wallet 的软件包,我在其中发布了迁移,这些迁移在某些表中有 JSON
列。
迁移:
public function up(): void
{
Schema::create($this->table(), function (Blueprint $table) {
$table->bigIncrements('id');
$table->morphs('payable');
$table->enum('type', ['deposit', 'withdraw'])->index();
$table->decimal('amount', 64, 0);
$table->boolean('confirmed');
$this->json($table, 'meta')->nullable();
$table->uuid('uuid')->unique();
$table->timestamps();
$table->index(['payable_type', 'payable_id', 'type'], 'payable_type_ind');
$table->index(['payable_type', 'payable_id', 'confirmed'], 'payable_confirmed_ind');
$table->index(['payable_type', 'payable_id', 'type', 'confirmed'], 'payable_type_confirmed_ind');
});
}
public function json(Blueprint $table, string $column): ColumnDefinition
{
$conn = DB::connection();
if ($conn instanceof MySqlConnection || $conn instanceof PostgresConnection) {
$pdo = $conn->getPdo();
try {
$sql = 'SELECT JSON_EXTRACT(\'[10, 20, [30, 40]]\', \'$[1]\');';
$prepare = $pdo->prepare($sql);
$prepare->fetch();
} catch (\Throwable $throwable) {
return $table->text($column);
}
}
return $table->json($column);
}
protected function table(): string
{
return (new Transaction())->getTable();
}
public function down(): void
{
Schema::drop($this->table());
}
此迁移在本地 xampp 服务器中运行良好,但我不明白为什么我的实时服务器出现错误。以下是我在尝试 运行 迁移时遇到的错误。
我尝试使用 $table->json('meta')->nullable();
这也没有用,尽管我认为这不是一件合适的事情,因为据我所知 $this->json()
调用 json
函数.
有些人还建议使用文本而不是 Json,但我觉得这不是解决方案。我们怎样才能解决这个问题?
请检查您的 MySQL 版本是否为 5.6 或低于该功能 json 将无法使用。 您应该使用 MySQL 5.7 或更高版本才能使用 json 功能。或者在 MySQL 5.6 或更低版本中使用“文本”作为列。