laravel 数据填充

来源:互联网 发布:电话假号软件 编辑:程序博客网 时间:2024/05/29 13:24
数据迁移创建数据迁移文件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 
原创粉丝点击