How to insert binary data into a database using Laravel?

I am trying to insert binary data into a PostgreSQL database using Laravel 4 and Eloquent ORM. In migration there is the following:

Schema::create('DataBlobs', function($table) {
    $table->increments('Id');
    $table->binary('Data');
});

After doing the migration, I verified that it creates a PostgreSQL BYTEA column. Big! So I put this in the database seeder function:

DB::table('DataBlobs')->delete();
// $d contains the binary data I want to insert into the database.
$d = base64_decode($raw_b64data);
$i = new DataBlob();
$i->Data = $d;
$i->save();

However, when I start the seeder, I get the following error:

SQLSTATE [22021]: character not in the repertoire: 7 ERROR: invalid byte sequence for encoding "UTF8": 0x89 (SQL: insert into the values ​​"DataBlobs" ("binary" that my console is trying to display)

var_dump bin2hex, $d , , , , Laravel UTF8, . Google, , Laravel. !

+4
1

DB:: unprepared ($ code);  DB:: ()

0

Source: https://habr.com/ru/post/1530743/


All Articles