LoginSignup
1
2

More than 5 years have passed since last update.

Laravel5.2 PostgreSQLでバイナリデータ(bytea型)をDBにInsertする

Last updated at Posted at 2016-07-23

##やりたいこと
Laravel5.2でPostgreSQLを使用してバイナリデータ(bytea型)をDBにInsert

##結論
・PDOを使用
・バイナリデータ(bytea型)をラージオブジェクト (LOB)として使用する
・SQLを直書きせずにクエリビルダーを使用してできるなら知りたい

##環境
・Laravel 5.2
・Laravel/homestead v0.5.0
  ・Ubuntu 16.04
  ・PHP7
  ・Nginx等
・使用するDB:PostgreSQL

##手順
1.PDOを使用
2.バイナリデータ(bytea型)をラージオブジェクト (LOB)としてInsert

migrationFile.php
    public function up()
    {
        Schema::create('files', function (Blueprint $table) {
            $table->increments('id');
            $table->binary('file_data');
        });
    }
HogeController.php
    //$hogeFileはリクエストから取得したFileデータ
    $fileData = file_get_contents($hogeFile);

    //Laravel/homesteadではPostgreSQLはpgsqlとして設定
    $db = DB::connection('pgsql')->getPdo();
    $stmt = $db->prepare("INSERT INTO files (file_data) VALUES (?)");
    $stmt->bindParam(1, $fileData, $db::PARAM_LOB);
 
    $stmt->execute();
    unset($db);

##参考
LARAVEL.IO:How to retrieve IMAGE from MSSQL server ?
PHPからPostgreSQL 9.3.2のbytea列(BLOB)に画像を登録、検索する

1
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
2