当前位置:首页 > CMS教程 > 其它CMS > 列表

详细Laravel5.5执行表迁移命令出现表为空的解决方案

发布:smiling 来源: PHP粉丝网  添加日期:2021-10-10 13:41:28 浏览: 评论:0 

今天在使用一个第三方包 laravel-admin 时,出现了这样的错误:SQLSTATE[42000]: Syntax error or access violation: 1103 Incorrect table name '',折腾了好久,终于知道了解决方法,原来是配置文件的缓存没有清理。

一、问题

vagrant@homestead:~/Code/laravel-shop$ php artisan admin:install

错误提示:

  1. In Connection.php line 664: 
  2.  
  3.   SQLSTATE[42000]: Syntax error or access violation: 1103 Incorrect table name '' (SQL: create table `` (`id` int uns 
  4.   igned not null auto_increment primary key, `username` varchar(190) not null, `password` varchar(60) not null, `name 
  5.   ` varchar(255) not null, `avatar` varchar(255) null, `remember_token` varchar(100) null, `created_at` timestamp nul 
  6.   l, `updated_at` timestamp null) default character set utf8mb4 collate utf8mb4_unicode_ci) 
  7.  
  8.  
  9. In Connection.php line 452: 
  10.  
  11.   SQLSTATE[42000]: Syntax error or access violation: 1103 Incorrect table name '' 

二、解决方案

database/migrations/2016_01_04_173148_create_admin_table.php

  1. <?php 
  2.  
  3. use Illuminate\Database\Migrations\Migration; 
  4. use Illuminate\Database\Schema\Blueprint; 
  5.  
  6. class CreateAdminTable extends Migration 
  7. { 
  8.   /** 
  9.    * Run the migrations. 
  10.    * 
  11.    * @return void 
  12.    */ 
  13.   public function up() 
  14.   { 
  15.     $connection = config('admin.database.connection') ?: config('database.default'); 
  16.  
  17.     // dd(app('config')); 
  18.     Schema::connection($connection)->create(config('admin.database.users_table'), function (Blueprint $table) { 
  19.       $table->increments('id'); 
  20.       $table->string('username', 190)->unique(); 
  21.       $table->string('password', 60); 
  22.       $table->string('name'); 
  23.       $table->string('avatar')->nullable(); 
  24.       $table->string('remember_token', 100)->nullable(); 
  25.       $table->timestamps(); 
  26.     }); 
  27.  
  28.     Schema::connection($connection)->create(config('admin.database.roles_table'), function (Blueprint $table) { 
  29.       $table->increments('id'); 
  30.       $table->string('name', 50)->unique(); 
  31.       $table->string('slug', 50); 
  32.       $table->timestamps(); 
  33.     }); 
  34.  
  35.     Schema::connection($connection)->create(config('admin.database.permissions_table'), function (Blueprint $table) { 
  36.       $table->increments('id'); 
  37.       $table->string('name', 50)->unique(); 
  38.       $table->string('slug', 50); 
  39.       $table->string('http_method')->nullable(); 
  40.       $table->text('http_path')->nullable(); 
  41.       $table->timestamps(); 
  42.     }); 
  43.  
  44.     Schema::connection($connection)->create(config('admin.database.menu_table'), function (Blueprint $table) { 
  45.       $table->increments('id'); 
  46.       $table->integer('parent_id')->default(0); 
  47.       $table->integer('order')->default(0); 
  48.       $table->string('title', 50); 
  49.       $table->string('icon', 50); 
  50.       $table->string('uri', 50)->nullable(); 
  51.  
  52.       $table->timestamps(); 
  53.     }); 
  54.  
  55.     Schema::connection($connection)->create(config('admin.database.role_users_table'), function (Blueprint $table) { 
  56.       $table->integer('role_id'); 
  57.       $table->integer('user_id'); 
  58.       $table->index(['role_id', 'user_id']); 
  59.       $table->timestamps(); 
  60.     }); 
  61.  
  62.     Schema::connection($connection)->create(config('admin.database.role_permissions_table'), function (Blueprint $table) { 
  63.       $table->integer('role_id'); 
  64.       $table->integer('permission_id'); 
  65.       $table->index(['role_id', 'permission_id']); 
  66.       $table->timestamps(); 
  67.     }); 
  68.  
  69.     Schema::connection($connection)->create(config('admin.database.user_permissions_table'), function (Blueprint $table) { 
  70.       $table->integer('user_id'); 
  71.       $table->integer('permission_id'); 
  72.       $table->index(['user_id', 'permission_id']); 
  73.       $table->timestamps(); 
  74.     }); 
  75.  
  76.     Schema::connection($connection)->create(config('admin.database.role_menu_table'), function (Blueprint $table) { 
  77.       $table->integer('role_id'); 
  78.       $table->integer('menu_id'); 
  79.       $table->index(['role_id', 'menu_id']); 
  80.       $table->timestamps(); 
  81.     }); 
  82.  
  83.     Schema::connection($connection)->create(config('admin.database.operation_log_table'), function (Blueprint $table) { 
  84.       $table->increments('id'); 
  85.       $table->integer('user_id'); 
  86.       $table->string('path'); 
  87.       $table->string('method', 10); 
  88.       $table->string('ip', 15); 
  89.       $table->text('input'); 
  90.       $table->index('user_id'); 
  91.       $table->timestamps(); 
  92.     }); 
  93.   } 
  94.  
  95.   /** 
  96.    * Reverse the migrations. 
  97.    * 
  98.    * @return void 
  99.    */ 
  100.   public function down() 
  101.   { 
  102.     $connection = config('admin.database.connection') ?: config('database.default'); 
  103.  
  104.     Schema::connection($connection)->dropIfExists(config('admin.database.users_table')); 
  105.     Schema::connection($connection)->dropIfExists(config('admin.database.roles_table')); 
  106.     Schema::connection($connection)->dropIfExists(config('admin.database.permissions_table')); 
  107.     Schema::connection($connection)->dropIfExists(config('admin.database.menu_table')); 
  108.     Schema::connection($connection)->dropIfExists(config('admin.database.user_permissions_table')); 
  109.     Schema::connection($connection)->dropIfExists(config('admin.database.role_users_table')); 
  110.     Schema::connection($connection)->dropIfExists(config('admin.database.role_permissions_table')); 
  111.     Schema::connection($connection)->dropIfExists(config('admin.database.role_menu_table')); 
  112.     Schema::connection($connection)->dropIfExists(config('admin.database.operation_log_table')); 
  113.   } 
  114. } 

清除配置文件缓存

vagrant@homestead:~/Code/laravel-shop$ php artisan config:cache

再次执行发布命令,就可以了:

  1. vagrant@homestead:~/Code/laravel-shop$ php artisan admin:install 
  2. Migrating: 2016_01_04_173148_create_admin_table 
  3. Migrated: 2016_01_04_173148_create_admin_table 
  4. Admin directory was created: /app/Admin 
  5. HomeController file was created: /app/Admin/Controllers/HomeController.php 
  6. ExampleController file was created: /app/Admin/Controllers/ExampleController.php 
  7. Bootstrap file was created: /app/Admin/bootstrap.php 
  8. Routes file was created: /app/Admin/routes.php 
  9. vagrant@homestead:~/Code/laravel-shop$

Tags: Laravel5 5 迁移命令

分享到: