数据迁移 创建数据迁移文件 php artisan make:migration create_users_table --create=users 追加列 php artisan make:migration add_votes_to_users_table --table=users 执行迁移文件 php artisan migrate 还原上一个迁移 php artisan migrate:rollback 还原所有迁移 php artisan migrate:reset 重新执行迁移文件 php artisan migrate:refresh 创建字段 $table->string('email'); 移除字段 $table->dropColumn('votes'); if (Schema::hasTable('users')) { // } if (Schema::hasColumn('users', 'email')) { // } if(!Schema::hasTable('goods')){ Schema::create('goods', function (Blueprint $table) { $table->increments('id'); $table->timestamps(); }); }else{ if(!Schema::hasColumn('goods', 'email')){ Schema::table('goods', function (Blueprint $table) { }); } } 数据填充 php artisan make:seeder UsersTableSeeder 在run方法添加测试数据 $data = []; for ($i = 0; $i < 100; $i++) { $tmp = []; $tmp['username'] = substr(str_shuffle('aashhshhshsahdbajdabdhabdbadADHADHU9WHQWHNOISCJZPKDA'), 0, 9); $tmp['email'] = substr(str_shuffle('aashhshhshsahdbajdabdhabdbadADHADHU9WHQWHNOISCJZPKDA'), 0, 6) . $i . '@163.com'; $tmp['password'] = substr(str_shuffle('aashhshhshsahdbajdabdhabdbadADHADHU9WHQWHNOISCJZPKDA'), 0, 6); $tmp['profile'] = '/Upload/20170409/14917762587600179.jpg'; $tmp['intro'] = substr(str_shuffle('aashhshhshsahdbajddabdhabadasdasdafdasfafasdasdasdasdasaddbadADHADHU9WHQWHNOISCJZPKDA'), 0, 60); $tmp['created_at'] = date('Y-m-d H:i:s'); $data[] = $tmp; } DB::table('users')->insert($data); 执行填充文件 php artisan db:seed --class=UserTableSeeder 第二种 填充方式 在DatabaseSeeder.php添加 public function run() { Model::unguard(); // $this->call(UserTableSeeder::class); $this->call(UserTableSeeder::class);//你的数据填充类 Model::reguard(); } 创建表和model的快捷方式
php artisan make:model Good --migration