gpt4 book ai didi

php - 在 ModelFactory Laravel 中播种多个自定义行/条目

转载 作者:行者123 更新时间:2023-12-04 14:00:46 24 4
gpt4 key购买 nike

如何在 Laravel 中使用 ModelFactory 播种多行?

ModelFactory.php我有以下代码:

$factory->define(App\User::class, function (Faker $faker) {
static $password;

return [
'name' => 'Admin',
'Description' => 'Administrators have full access to everything.'
];
});

如何在不使用原始表达式的情况下添加以下数组?
[
'name' => 'Admin',
'description' => 'Administrators have full access to everything.',
],
[
'name' => 'User',
'description' => 'User have normal access.',
],

谢谢

最佳答案

假设您想在数据库中添加 100 个用户。

创建一个 UserFactory.phpdatabase/factories :

<?php

use Faker\Generator as Faker;

$factory->define(App\User::class, function (Faker $faker) {

return [
'name' => 'Admin',
'Description' => 'Administrators have full access to everything.'
];
});

然后,在 database/seeds/DatabaseSeeder.php :

<?php

use Illuminate\Database\Seeder;

class DatabaseSeeder extends Seeder
{
/**
* Run the database seeds.
*
* @return void
*/
public function run()
{
factory(App\User::class, 100)->create();
}
}

您可以找到更多详情 about seeding in the Laravel official documentation .

关于php - 在 ModelFactory Laravel 中播种多个自定义行/条目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46420005/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com