```php define(App\Topic::class, function (Faker $faker) { $items = [1, 2, 3, 4]; shuffle($items); $random_date = $faker->dateTimeBetween('-3 days', '+3 days'); $num1 = rand(1, 99); $num2 = rand(1, 99); return [ 'topic' => $num1 . " + " . $num2, 'opt' . $items[0] => $num1 + $num2, 'opt' . $items[1] => $num1 . $num2, 'opt' . $items[2] => rand(1, 99), 'opt' . $items[3] => rand(1, 999), 'ans' => $items[0], 'created_at' => $random_date, 'updated_at' => $random_date, ]; }); ``` ``` 4. 其中`shuffle()`是為了給選項隨機排序,並指定第一個選項`$items[0]`為正確答案,避免答案都是固定的選項。 5. 完整的Faker用法可以參考: 6. 這裡有各種方法的使用範例: 7. 若欲產生中文資料,請修改`\專案\vendor\fzaninotto\faker\src\Faker\Factory.php` ``` ```php make() ``` 10. 然後建立5題 ```bash factory(App\Topic::class,5)->make() ``` 11. 建立5題並指定exam\_id為1,然後存入資料庫: ```bash factory(App\Topic::class,5)->create(['exam_id' => 3]) ``` 12. 可以利用此方式來建立大量測設內容,若工廠內容有改變,請離開tinker後再重新進入執行。 ### [](https://github.com/tad0616/exam56/commit/edb6d40236a6dd5e8e3030709994373a066a64f9)
```php make() ``` 10. 然後建立5題 ```bash factory(App\Topic::class,5)->make() ``` 11. 建立5題並指定exam\_id為1,然後存入資料庫: ```bash factory(App\Topic::class,5)->create(['exam_id' => 3]) ``` 12. 可以利用此方式來建立大量測設內容,若工廠內容有改變,請離開tinker後再重新進入執行。 ### [](https://github.com/tad0616/exam56/commit/edb6d40236a6dd5e8e3030709994373a066a64f9)
進階搜尋