Laravel-使用Postgre Bytea Blob字段 [英] Laravel - using a postgre bytea blob field
问题描述
我在Laravel安装上使用PostgreSQL.一个表具有一个bytea类型字段,该字段用于存储二进制数据(base64_encoded文件内容).
I am using PostgreSQL on a Laravel installation. A table has a bytea type field which is being used to store binary data (base64_encoded file contents).
当我使用Eloquent检索表时,在此字段中返回一个资源类型变量.
When I use Eloquent to retrieve the table I get a resource type variable being returned in this field.
我该如何将其检索为字符串?
How can I rather retrieve this as a string?
$raw = Media::where('id','=',$id)->first();
$raw->file_data = base64_decode($raw->file_data); // doesn't work
推荐答案
由于该问题的作者未在答案中发布详细信息,因此我将在此处发布我的发现.
As the author of this question did not post the details to the answer, I will post my findings here.
由于返回的字段是流的句柄,因此您可以使用 stream_get_contents 函数将值读取为字符串,然后可以使用pg_unescape_bytea来获取bytea数据的实际值.如果您希望以HTML显示bytea数据,最后使用htmlspecialchars函数.
As the returned field is a handle to a stream you can use the stream_get_contents function to read the value into a string, you can then use pg_unescape_bytea to get the actual value of the bytea data. Finally use the htmlspecialchars function if you wish to display the bytea data in HTML.
示例代码:
$my_bytea = stream_get_contents($resource);
$my_string = pg_unescape_bytea($my_bytea);
$html_data = htmlspecialchars($my_string);
这篇关于Laravel-使用Postgre Bytea Blob字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!