php artisan make:factory StatusFactory
public function definition()
{
$date_time = $this->faker->date . ‘ ‘ . $this->faker->time;
return [
‘user_id‘ => $this->faker->randomElement([‘1‘, ‘2‘, ‘3‘]),
‘content‘ => $this->faker->text(),
‘created_at‘ => $date_time,
‘updated_at‘ => $date_time
];
}
php artisan make:seeder StatusesTableSeeder
public function run()
{
Status::factory()->count(100)->create();
}
php artisan db:seed --class=StatusesTableSeeder
【laravel】利用模型工厂函数和数据填充,为前3个用户生成随机100条数据
原文:https://www.cnblogs.com/mflnhg/p/14620030.html