Laravel 透過遷移指令碼建立MySQL檢視

helong3314發表於2021-05-12
php artisan make:migration create_view_users_table

生成檔案

Created Migration: 2021_05_12_125514_create_view_users_table

class CreateViewUsersTable extends Migration
{
  public function up()
    {
       \Illuminate\Support\Facades\DB::statement(" CREATE VIEW view_users AS SELECT * FROM  ".with(new \Modules\Auth\Entities\User())->getTable());

    }

  public function down()
    {
        DB::statement( 'DROP VIEW view_users' );
    }
}

然後你可以建立一個模型來訪問它:

class MyView extends Eloquent {

protected $table = 'view_users';

}

本作品採用《CC 協議》,轉載必須註明作者和本文連結

相關文章