Advertisement
tomjerry741

Migration

Nov 22nd, 2020 (edited)
754
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 4.48 KB | None | 0 0
  1. <?php
  2.  
  3. use Illuminate\Database\Migrations\Migration;
  4. use Illuminate\Database\Schema\Blueprint;
  5. use Illuminate\Support\Facades\Schema;
  6.  
  7. class CreateAdminTables extends Migration
  8. {
  9.     public function getConnection()
  10.     {
  11.         return config('database.connection') ?: config('database.default');
  12.     }
  13.  
  14.     public function config($key)
  15.     {
  16.         return config('admin.'.$key);
  17.     }
  18.  
  19.     /**
  20.      * Run the migrations.
  21.      *
  22.      * @return void
  23.      */
  24.     public function up()
  25.     {
  26.         Schema::create($this->config('database.users_table'), function (Blueprint $table) {
  27.             $table->bigIncrements('id');
  28.             $table->string('username', 120)->unique();
  29.             $table->string('password', 80);
  30.             $table->string('name');
  31.             $table->string('avatar')->nullable();
  32.             $table->string('remember_token', 100)->nullable();
  33.             $table->timestamps();
  34.         });
  35.  
  36.         Schema::create($this->config('database.roles_table'), function (Blueprint $table) {
  37.             $table->bigIncrements('id');
  38.             $table->string('name', 50);
  39.             $table->string('slug', 50)->unique();
  40.             $table->timestamps();
  41.         });
  42.  
  43.         Schema::create($this->config('database.permissions_table'), function (Blueprint $table) {
  44.             $table->bigIncrements('id');
  45.             $table->string('name', 50);
  46.             $table->string('slug', 50)->unique();
  47.             $table->string('http_method')->nullable();
  48.             $table->text('http_path')->nullable();
  49.             $table->integer('order')->default(0);
  50.             $table->bigInteger('parent_id')->default(0);
  51.             $table->timestamps();
  52.         });
  53.  
  54.         Schema::create($this->config('database.menu_table'), function (Blueprint $table) {
  55.             $table->bigIncrements('id');
  56.             $table->bigInteger('parent_id')->default(0);
  57.             $table->integer('order')->default(0);
  58.             $table->string('title', 50);
  59.             $table->string('icon', 50)->nullable();
  60.             $table->string('uri', 50)->nullable();
  61.  
  62.             $table->timestamps();
  63.         });
  64.  
  65.         Schema::create($this->config('database.role_users_table'), function (Blueprint $table) {
  66.             $table->bigInteger('role_id');
  67.             $table->bigInteger('user_id');
  68.             $table->unique(['role_id', 'user_id']);
  69.             $table->timestamps();
  70.         });
  71.  
  72.         Schema::create($this->config('database.role_permissions_table'), function (Blueprint $table) {
  73.             $table->bigInteger('role_id');
  74.             $table->bigInteger('permission_id');
  75.             $table->unique(['role_id', 'permission_id']);
  76.             $table->timestamps();
  77.         });
  78.  
  79.         Schema::create($this->config('database.role_menu_table'), function (Blueprint $table) {
  80.             $table->bigInteger('role_id');
  81.             $table->bigInteger('menu_id');
  82.             $table->unique(['role_id', 'menu_id']);
  83.             $table->timestamps();
  84.         });
  85.  
  86.         Schema::create($this->config('database.permission_menu_table'), function (Blueprint $table) {
  87.             $table->bigInteger('permission_id');
  88.             $table->bigInteger('menu_id');
  89.             $table->unique(['permission_id', 'menu_id']);
  90.             $table->timestamps();
  91.         });
  92.  
  93.         Schema::create($this->config('database.operation_log_table'), function (Blueprint $table) {
  94.             $table->bigIncrements('id');
  95.             $table->bigInteger('user_id');
  96.             $table->string('path');
  97.             $table->string('method', 10);
  98.             $table->string('ip');
  99.             $table->text('input');
  100.             $table->index('user_id');
  101.             $table->timestamps();
  102.         });
  103.     }
  104.  
  105.     /**
  106.      * Reverse the migrations.
  107.      *
  108.      * @return void
  109.      */
  110.     public function down()
  111.     {
  112.         Schema::dropIfExists($this->config('database.users_table'));
  113.         Schema::dropIfExists($this->config('database.roles_table'));
  114.         Schema::dropIfExists($this->config('database.permissions_table'));
  115.         Schema::dropIfExists($this->config('database.menu_table'));
  116.         Schema::dropIfExists($this->config('database.role_users_table'));
  117.         Schema::dropIfExists($this->config('database.role_permissions_table'));
  118.         Schema::dropIfExists($this->config('database.role_menu_table'));
  119.         Schema::dropIfExists($this->config('database.permission_menu_table'));
  120.         Schema::dropIfExists($this->config('database.operation_log_table'));
  121.     }
  122. }
  123.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement