LoginSignup
21
19

More than 5 years have passed since last update.

Laravel マイグレーションで View(CREATE VIEW)を作る

Posted at

Schema クラスは対応していないので、DB::statement() で、SQL を流す。

以下、PostgreSQL の例。

<?php

use Illuminate\Database\Migrations\Migration;

class CreateHoge extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        $sql = <<<EOT
CREATE VIEW hoge AS
SELECT column1,column2
FROM foo
EOT;

        DB::statement($sql);
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        DB::statement('DROP VIEW hoge');
    }
}
21
19
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
21
19