5

For some of my tables, I'd like to insert a fixed amount of rows with specific data.

This is my categories factory:

$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'
        ]
    ];
});

This is the seeder:

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

I get this error: preg_match() expects parameter 2 to be string, array given

Is there a way to insert a fixed amount of static information into certain tables using seeding and factories?

Prafulla Kumar Sahu
  • 9,321
  • 11
  • 68
  • 105
good_afternoon
  • 1,529
  • 1
  • 12
  • 41

2 Answers2

8

I think you want to use Seeder with static values, if I am correct you should use

Define Category 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);
        }
    }
}

And in DatabaseSeeder

<?php

use Illuminate\Database\Seeder;

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

Now run php artisan db:seed and it will be done.

Prafulla Kumar Sahu
  • 9,321
  • 11
  • 68
  • 105
  • Thank you! I seemed to have misunderstood the need for factories. This worked perfectly – good_afternoon May 12 '19 at 14:56
  • I tend to use Eloquent's `firstOrCreate` method for seeders to avoid duplicates when calling `php artisan db:seed` by mistake, for instance: `foreach ($categories as $category) { Category::firstOrCreate(array_only($category, ['name', 'slug']), array_except($category, ['name', 'slug])); }` assuming that `name` and `slug` columns are unique. – zoltalar May 13 '19 at 08:20
  • @zoltalar that is okay, when you are putting static value, you know what you are putting most of the time. – Prafulla Kumar Sahu May 13 '19 at 08:44
0

The answer of @Prafulla Kumar Sahu is by seeder, but you can override your factory values by:

$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'
            ]);
francisco
  • 1,387
  • 2
  • 12
  • 23