Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

TheBlueDragon's avatar

How to apply this scenario in Faker Laravel 5.1

Hello every one,

iam trying to make fake data for testing the actual app without manually add every thing as i need a min of 100 question =( my app is quiz

in real demo from control panel i have a form

that have a question test + 3 text input as answer and 3 radio button for choosing the correct answer and i have other field for image and select menu

for making question factory is ok i make it

$factory->define(Ideal\Questions::class, function (Faker\Generator $faker) {
return [
    'question' => $faker->sentence(5),
    'image' => $faker->imageUrl(250,250),
    'language_id' => Ideal\Language::all()->random()->id,
];
});

but for the answers

how to do it

as i need every 3 answer to be with same question id and also only 1 answer of these 3 should be correct

$factory->define(Ideal\Answers::class, function (Faker\Generator $faker) {
return [
    'answer' => $faker->word,
    'question_id' => Ideal\Questions::all()->random()->id,
    'correct' => $faker->boolean(15),
];
});

as u can see this is what i made and stop how i can make it work >_< as a real life demo

who to make 3 answers with same question id and only one of them is correct

0 likes
9 replies
nfauchelle's avatar
Level 11

Hi there,

IMHO. Don't use the factory quite like that, and I would use a table seeder.

Anyway... Remove the question_id from the Answers factory, I would set them all to be correct => false

Do the relation like http://laravel.com/docs/master/testing#model-factories -> Adding Relations To Models

You can then get a random one from the relation to set the correct one.

http://laravel.com/api/5.1/Illuminate/Database/Eloquent/Collection.html#method_random

$randomAnswer = $quenstion->answers->random();
$randomAnswer->correct = true;
$randomAnswer->save();

Hope it helps.

1 like
shez1983's avatar

you can also do some logic inside the AnswerSeeder...

  1. get all the question_ids in an array
  2. loop through each,
  3. inner loop which will call the answers factory creating one record at a time
  4. before this inner loop, get a random id from 0 - 2 (thats what the inner loop $i/$j will be based on)
  5. inside the inner loop a check if current id == $i.. and if so pass in to answer factory correct => 1
TheBlueDragon's avatar

@nfauchelle i try to do this like factory(Ideal\Questions::class,5) ->create() ->each(function($q){ $q->answers() ->saveMany(factory(Ideal\Answers::class,3) ->make());});

but its give me

SQLSTATE[42S22]: Column not found: 1054 Unknown column 'questions_id' in 'field list' (SQL: insert into `answers` (`answer`,         `correct`, `questions_id`, `updated_at`, `created_at`) values (est, 0, 104, 2015-11-30 07:44:57, 2015-11-30 07:44:57))

and i think this because the removed question_id from the Answer Factory

TheBlueDragon's avatar

@nfauchelle i change my database structures for question_id to be questions_id i dont know why it through this error

maybe its from the factory model it self it deal with the model name it self+id as my model name is Questions.php maybe thats the issue that come from

nfauchelle's avatar

@TheBlueDragon

Well, your model is also plural when models are typically singular. That means for the relation it will be looking for a plural table name.

You can override that in your relation, but it might help to make Question model and Answer model Then in your Question model

function answers()
{
    return $this->hasMany('App\Answer');
}

and in your Answer model

function question()
{
    return $this->belongsTo('App\Question');
}
TheBlueDragon's avatar

@nfauchelle i see, ya my models like this anyway i just change my schema to make it work not a big deal

thank you very much for help ^^

Please or to participate in this conversation.