迁移和填充
迁移:即创建表格
1.生成迁移(php artisan help)
php artisan make:migration create_member_table
2.在文件夹 /database/migrations 生成文件
<?php use Illuminate\Database\Schema\Blueprint; use Illuminate\Database\Migrations\Migration; class CreateMemberTable extends Migration { /** * Run the migrations. * * @return void */ public function up() { //自定义数据库字段 Schema::create('member', function (Blueprint $table) { $table->increments('uid'); $table->string('mobile',11); $table->string('username'); $table->string('password',40); $table->string('status'); $table->decimal('money',7,2); $table->timestamp('regtime'); $table->timestamp('lasttime'); }); } /** * Reverse the migrations. * * @return void */ public function down() { // } }
3.生成数据库
php artisan migrate
填充数据
1.生成数据填充类
php artisan make:seeder MemberTableSeeder
2.在文件夹 /database/seeds 生成文件
<?php use Illuminate\Database\Seeder; class MerberTableSeeder extends Seeder { /** * Run the database seeds. * * @return void */ public function run() { //生成50条 for ($i=0; $i < 50 ; $i++) { DB::table('member')->insert([ 'mobile' => str_random(11), 'username' => str_random(10) , 'password' => bcrypt('secret'), 'status' => array_rand(array(1,10,20),1), 'money' => 1, 'regtime' => date('Y-m-d H:i:s'), 'lasttime' => date('Y-m-d H:i:s'), ]); } } }
3.生成数据
php artisan db:seed //所有seeds 下的类 都将运行
php artisan db:seed --class=MerberTableSeeder //指定填充类