1.0 生成Seeder
php artisan make:seeder MemberTableSeeder
2.0 写代码
use Illuminate\Database\Seeder;
class MemberTableSeeder extends Seeder
{
/**
* Run the database seeds.
*
* @return void
*/
public function run()
{
$data = [];
//不用引入DB类,直接使用就可以了
$faker = \Faker\Factory::create('zh_CN');
for($i = 0; $i < 20; $i++){
$data[] = [
'username' => $faker -> Name,//Name是中文的,userName是英文的
'password' => bcrypt('liyuyu'),//不可逆加密
'avatar' => $faker -> imageUrl,
'updated_at' => $faker -> DateTime,
'created_at' => $faker -> DateTime,
];
}
DB::table('member') -> insert($data);
}
}
3.0 生成数据,如果数据较多,则较耗费时间
php artisan db:seed --class=MemberTableSeeder