laraver6-数据填充

1.创建模型工厂
php artisan make:factory AdminFactory --model=Admin

<?php

/** @var \Illuminate\Database\Eloquent\Factory $factory */

use App\Models\System\Admin;
use Faker\Generator as Faker;

$factory->define(Admin::class, function (Faker $faker) {
return [
'name' => $faker->name,
'account' => $faker->unique()->name,
'password' => bcrypt(Admin::$defaultPassword),
];
});
2.创建seeders
php artisan make:seeder PostTableSeeder
完成 seeder 类的编写之后,你可能需要使用 dump-autoload 命令重新生成 Composer 的自动加载器
<?php

use Illuminate\Database\Seeder;
use App\Models\System\Admin;

class AdminTableSeeder extends Seeder
{
public function run()
{
factory(Admin::class, 2)->create();
}
}
3. 将新加的seeder添加到DatabaseSeeder.php

<?php

use Illuminate\Database\Seeder;

class DatabaseSeeder extends Seeder
{
    public function run()
    {
        $this->call([
            AdminTableSeeder::class
        ]);
    }
}
4.执行命令

php artisan db:seed

php artisan db:seed --class=AdminTableSeeder    //指定seeder

 

posted @ 2022-05-18 09:53  CanyingV  阅读(20)  评论(0编辑  收藏  举报