编码"UTF8"的字节序列无效:0x89 PostgreSQL Laravel 8



我在Laravel 8的windows 10机器上工作
完全错误:

SQLSTATE[22021]: Character not in repertoire: 7 ERROR:  invalid byte sequence for encoding "UTF8": 0x89

我想在PostgreSQL数据库中插入一个二维码。在我的本地主机上,这与MySQL数据库配合得很好,但在我的远程主机Heroku上,我收到了以下错误:invalid byte sequence for encoding "UTF8": 0x89

我曾尝试更改configdatabase.php中远程数据库的编码,以匹配本地数据库(即utf8mb4(的相同编码,但这不起作用。

生成二维码并将其存储在数据库databaseseedersDatabaseSeeder.php

use SimpleSoftwareIOQrCodeFacadesQrCode;
use AppModelsQrCodeClient;
$client1qr = QrCode::format('png')->encoding('utf8')->size(100)->generate($client1->id);
QrCodeClient::create([
'client_id' => $client1->id,
'qrcode' => $client1qr,
]);

QrCode::format('png')->encoding('utf8')->size(100)->generate($client1->id);起初只是QrCode::format('png')->size(100)->generate($client1->id);,但仍然给出相同的误差

QrCodeClient型号的迁移文件

Schema::create('qr_code_clients', function (Blueprint $table) {
$table->id();
$table->foreignId('client_id');
$table->binary('qrcode');
$table->index('client_id');
$table->timestamps();
});

pgsql的配置

'pgsql' => [
'driver' => 'pgsql',
'url' => env('DATABASE_URL'),
'host' => env('DB_HOST', '127.0.0.1'),
'port' => env('DB_PORT', '5432'),
'database' => env('DB_DATABASE', 'forge'),
'username' => env('DB_USERNAME', 'forge'),
'password' => env('DB_PASSWORD', ''),
'charset' => 'utf8',
'prefix' => '',
'prefix_indexes' => true,
'schema' => 'public',
'sslmode' => 'prefer',
],

我已经研究了关于这个话题的其他问题,但我并没有找到任何有用的信息来解决我的具体问题
有人能告诉我需要做什么才能将PNG文件以二进制形式存储在PostgreSQL数据库中吗?编辑:解决方案
我已更改

$client1qr = QrCode::format('png')->encoding('utf8')->size(100)->generate($client1->id);
QrCodeClient::create([
'client_id' => $client1->id,
'qrcode' => $client1qr,
]);

$client1qr = QrCode::format('png')->generate($client1->id);
$escaped1 = base64_encode($client1qr);
QrCodeClient::create([
'client_id' => $client1->id,
'qrcode' => $escaped1,
]);

然后在前端显示:

<?php
$my_bytea = stream_get_contents($bed->client()->first()->QrCode()->first()->qrcode);
$my_string = pg_unescape_bytea($my_bytea);
$html_data = htmlspecialchars($my_string);
?>
<td><img src="data:image/png;base64,{{$html_data}}"></td>

更改

$client1qr = QrCode::format('png')->encoding('utf8')->size(100)->generate($client1->id);
QrCodeClient::create([
'client_id' => $client1->id,
'qrcode' => $client1qr,
]);

$client1qr = QrCode::format('png')->generate($client1->id);
$escaped1 = base64_encode($client1qr);
QrCodeClient::create([
'client_id' => $client1->id,
'qrcode' => $escaped1,
]);

然后在前端显示:

<?php
$my_bytea = stream_get_contents($bed->client()->first()->QrCode()->first()->qrcode);
$my_string = pg_unescape_bytea($my_bytea);
$html_data = htmlspecialchars($my_string);
?>
<td><img src="data:image/png;base64,{{$html_data}}"></td>

最新更新