Mark one row different in `factory->create()` while rest have same value

Published 2 months ago by danjas

OK, so I a database table of questions and answers.

Each question gets 4 answers (or however many they decide).

So let's say I have this seeder:

    public function run()
    {
        factory(Category::class, 5)->create()->each(function ($s) {
            factory(Questions::class, 5)->create(
                ['category_id' => $s->id]
            )->each(function ($q) {
                factory(Answer::class, 4)->create(
                    [
                        'questions_id' => $q->id
                        'correct' = > ??????
                    ]
                );
            });
        });
    }

As you can see, 5 categories get created. For each category, we get 5 questions. For each question, we get 4 answers.

My situation: I need to have ONE of Answer::class created of to be marked as 1 and the rest 0 in the correct column.

How would I do this?

Best Answer (As Selected By danjas)
Dry7

@danjas

    public function run()
    {
        factory(Category::class, 5)->create()->each(function ($s) {
            factory(Questions::class, 5)->create(
                ['category_id' => $s->id]
            )->each(function ($q) {
                factory(Answer::class, 1)->create(
                    [
                        'questions_id' => $q->id
                        'correct' = > 1
                    ]
                );
                factory(Answer::class, 3)->create(
                    [
                        'questions_id' => $q->id
                        'correct' = > 0
                    ]
                );
            });
        });
    }

or use

static $i;
danjas

I was able to just replace the inside of each($q) with this block:

                $randomAnswer = [0, 0, 0, 1];
                shuffle($randomAnswer);

                for ($i=0; $i < 4; $i++) {
                    factory(Answer::class, 1)->create(
                        [
                            'questions_id' => $q->id,
                            'correct' => $randomAnswer[$i]
                        ]
                    );
                }

and it seems to work. Doesn't seem clean, though...

Dry7
Dry7
2 months ago (134,770 XP)

@danjas

    public function run()
    {
        factory(Category::class, 5)->create()->each(function ($s) {
            factory(Questions::class, 5)->create(
                ['category_id' => $s->id]
            )->each(function ($q) {
                factory(Answer::class, 1)->create(
                    [
                        'questions_id' => $q->id
                        'correct' = > 1
                    ]
                );
                factory(Answer::class, 3)->create(
                    [
                        'questions_id' => $q->id
                        'correct' = > 0
                    ]
                );
            });
        });
    }

or use

static $i;
danjas

@Dry7 You know... some days I just miss the easiest solutions. D'oh! Why didn't I think of that. Thanks.

Please sign in or create an account to participate in this conversation.