Laravel 通过迁移脚本创建MySQL视图

Laravel框架
389
0
0
2022-04-13
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';
}