【问题标题】:Laravel Factories and Seeding: Static array of arraysLaravel 工厂和播种:数组的静态数组
【发布时间】:2019-09-29 16:54:46
【问题描述】:

对于我的一些表,我想插入具有特定数据的固定数量的行。

这是我的分类工厂:

$factory->define(Category::class, function (Faker $faker) {
    return [
        [
            'name' => 'Politics',
            'slug' => 'politics',
            'description' => 'To discuss politics'
        ],
        [
            'name' => 'News and Events',
            'slug' => 'news',
            'description' => 'To discuss news and world events'
        ],
        [
            'name' => 'Food and Cooking',
            'slug' => 'cooking',
            'description' => 'To discuss cooking and food'
        ],
        [
            'name' => "Animals and Nature",
            'slug' => 'animals-nature',
            'description' => 'To discuss politics'
        ]
    ];
});

这是播种机:

public function run() {
   factory(App\Category::class, 1)->create();
}

我收到此错误:preg_match() expects parameter 2 to be string, array given

有没有办法使用种子和工厂将固定数量的静态信息插入到某些表中?

【问题讨论】:

  • Faker 用于制作虚假数据.. 改用播种机
  • @ZeroOne,我刚刚从另一个使用 Faker 的工厂复制了一个工厂。旨在删除它

标签: php laravel laravel-5 seeding laravel-seeding


【解决方案1】:

@Prafulla Kumar Sahu 的答案是播种机,但您可以通过 override your factory 值:

$category = factory(App\Category::class)->make([
                'name' => 'Politics',
                'slug' => 'politics',
                'description' => 'To discuss politics'
            ]);
$category = factory(App\Category::class)->make([
                'name' => 'News and Events',
                'slug' => 'news',
                'description' => 'To discuss news and world events'
            ]);
$category = factory(App\Category::class)->make([
                'name' => 'Food and Cooking',
                'slug' => 'cooking',
                'description' => 'To discuss cooking and food'
            ]);
$category = factory(App\Category::class)->make([
                'name' => "Animals and Nature",
                'slug' => 'animals-nature',
                'description' => 'To discuss politics'
            ]);

【讨论】:

    【解决方案2】:

    我认为您想使用带有静态值的 Seeder,如果我是正确的,您应该使用

    定义类别播种器

    <?php
    use Illuminate\Database\Seeder;
    use App\Category;
    
    class CategorySeeder extends Seeder
    {
        /**
         * Run the database seeds.
         *
         * @return void
         */
        public function run()
        {
            $categories = [
                 [
                'name' => 'Politics',
                'slug' => 'politics',
                'description' => 'To discuss politics'
            ],
            [
                'name' => 'News and Events',
                'slug' => 'news',
                'description' => 'To discuss news and world events'
            ],
            [
                'name' => 'Food and Cooking',
                'slug' => 'cooking',
                'description' => 'To discuss cooking and food'
            ],
            [
                'name' => "Animals and Nature",
                'slug' => 'animals-nature',
                'description' => 'To discuss politics'
            ]
            ];
    
            foreach ($categories as $category) {
                Category::create($category);
            }
        }
    }
    

    在 DatabaseSeeder 中

    <?php
    
    use Illuminate\Database\Seeder;
    
    class DatabaseSeeder extends Seeder
    {
        /**
         * Seed the application's database.
         *
         * @return void
         */
        public function run()
        {
            $this->call(CategorySeeder::class);
        }
    }
    

    现在运行php artisan db:seed,它就会完成。

    【讨论】:

    • 谢谢!我似乎误解了工厂的必要性。这工作得很好
    • 我倾向于使用 Eloquent 的 firstOrCreate 方法让播种者在错误地调用 php artisan db:seed 时避免重复,例如:foreach ($categories as $category) { Category::firstOrCreate(array_only($category, ['name', 'slug']), array_except($category, ['name', 'slug])); } 假设 nameslug 列是唯一的。
    • @zoltalar 没关系,当你放置静态值时,你知道大部分时间你在放置什么。
    猜你喜欢
    • 2017-08-02
    • 2015-09-16
    • 2018-08-18
    • 2020-03-18
    • 2020-01-29
    • 2017-02-10
    • 1970-01-01
    • 2021-06-11
    • 2017-04-07
    相关资源
    最近更新 更多