*/ class QuestionFactory extends Factory { protected $model = Question::class; /** * Define the model's default state. * * @return array */ public function definition(): array { $cate = $this->faker->randomElement(QuestionCate::class); $options = []; $max = 4; $index = []; if ($cate == QuestionCate::Radio) { $index = [$this->faker->randomElement(range(0, $max- 1))]; } else if ($cate == QuestionCate::Checkbox) { $index = $this->faker->randomElements(range(0, $max- 1), $this->faker->numberBetween(2, 4)); } for($i = 0; $i < $max; $i++) { array_push($options, ['text' => $this->faker->word, 'is_true' => in_array($i, $index)]); } return [ 'title' => $this->faker->sentence, 'cate' => $cate, 'options' => $options, ]; } }