I'm trying to insert binary data into a PostgreSQL database using Laravel 4 and its Eloquent ORM. I have the following in a migration:
Schema::create('DataBlobs', function($table) {
$table->increments('Id');
$table->binary('Data');
});
After running the migration, I've verified that it creates a PostgreSQL BYTEA column. Great! So I put this in a 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();
When I run that seeder, however, I get the following error:
SQLSTATE[22021]: Character not in repertoire: 7 ERROR: invalid byte sequence for encoding "UTF8": 0x89 (SQL: insert into "DataBlobs" ("Data") values (binary gibberish that my console tries to render)
I've validated using var_dump and bin2hex that $d is in fact a binary string that contains the data I want, but from the error, it looks like Laravel is trying to insert the binary data as a UTF8 string instead of as binary data. I've been googling for a day now, and I can't find any examples of how to insert binary data into a database using Laravel. Any help would be appreciated!