美文网首页
[Laravel5.3教程03] 基础02 -migratio

[Laravel5.3教程03] 基础02 -migratio

作者: 7c03aed0f01f | 来源:发表于2017-01-08 14:46 被阅读21次

    一、创建数据库的 填充数据的文件

    php artisan make:seeder UsersTableSeeder
    

    二、seeder 填充文件

    /database/seeds/UsersTableSeeder.php

         public function run() {
              factory('App\User')->create([ // 
                   'mobile'     => '13811112057',
                   'pwd'        => bcrypt('123456'),
                   'tpwd'       => bcrypt('123456'),
                   'userIcon'   => '1.png',
                   'framName'   => '狗蛋农场',
                   'recommen'   => 0,
                   'eggCount'   => 5.00,
                   'feedsCount' => '4.34',
              ]);
              factory('App\User')->create([ // 
                   'mobile'     => '13811111213',
                   'pwd'        => bcrypt('123456'),
                   'tpwd'       => bcrypt('123456'),
                   'userIcon'   => '1.png',
                   'framName'   => '狗蛋农场',
                   'recommen'   => 13811112057,
                   'eggCount'   => 5.00,
                   'feedsCount' => '4.34',
              ]);
              factory('App\User',1)->create([ // 随机1 顶级会员
                   'pwd'        => bcrypt('123456'),
                   'tpwd'       => bcrypt('123456'),
                   'recommen'   => 0,
              ]);
              factory('App\User',3)->create([ // 随机2 下级
                   'pwd'        => bcrypt('123456'),
                   'tpwd'       => bcrypt('123456'),
                   'recommen'   => 13811112057,
              ]);
              factory('App\User',3)->create([ // 随机3 下级
                   'pwd'        => bcrypt('123456'),
                   'tpwd'       => bcrypt('123456'),
                   'recommen'   => 13811111213,
              ]);
         }
    

    三、开启 允许填充数据的文件

    /database/seeds/DatabaseSeeder.php

        public function run()
        {
            $this->call(UsersTableSeeder::class);
        }
    

    四、模型工厂 设置自动填充数据的规则

    官方规则文档

    中文的规则源文件 /vendor/fzaninotto/faker/src/Faker/Provider/zh_CN/Person.php

    /database/factories/ModelFactory.php

    $faker = Faker\Factory::create('zh_CN'); // 设置中文
    $factory->define(App\User::class, function() use ($faker) {
         static $password;
         return [
              'mobile'         => $faker->phoneNumber,
              'pwd'            => $password ?: $password = bcrypt('123321'),
              'tpwd'           => $password ?: $password = bcrypt('123321'),
              'userIcon'       => '1.png',
              'framName'       => $faker->lastName . $faker->firstNameFemale .'的农场',
              'eggCount'       => $faker->randomFloat(2, 0.00, 99.99),
              'feedsCount'     => $faker->randomFloat(2, 0.00, 99.99),
              'remember_token' => str_random(10),
    
         ];
    });
    
    

    五、执行

    执行 UsersTableSeeder 文件

    php artisan db:seed --class=UsersTableSeeder
    

    也可以用 tinker 操作

    php artisan tinker
    

    执行所有

    php artisan migrate:refresh --seed
    

    相关文章

      网友评论

          本文标题:[Laravel5.3教程03] 基础02 -migratio

          本文链接:https://www.haomeiwen.com/subject/qiuqbttx.html