Skip to content

Commit a350007

Browse files
committed
new: database seeder and factory for user
1 parent f95b819 commit a350007

File tree

3 files changed

+60
-0
lines changed

3 files changed

+60
-0
lines changed

database/Factories/UserFactory.php

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
3+
namespace LaraCore\Database\Factories;
4+
5+
use LaraCore\App\Models\Users;
6+
use LaraCore\Framework\Factories\Factory;
7+
8+
class UserFactory extends Factory
9+
{
10+
public function definition()
11+
{
12+
$quizJson = file_get_contents(base_path('quiz.json'));
13+
$quiz = json_decode($quizJson, true);
14+
return [
15+
'questions' => json_encode($quiz[2])
16+
];
17+
}
18+
19+
public function create()
20+
{
21+
$data = $this->definition();
22+
$this->make(Users::class, $data);
23+
}
24+
public static function new()
25+
{
26+
return new static;
27+
}
28+
29+
}

database/Seeders/DatabaseSeeder.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
3+
namespace LaraCore\Database\Seeders;
4+
5+
use LaraCore\Framework\Seeders\Seeder;
6+
7+
class DatabaseSeeder extends Seeder
8+
{
9+
public function run()
10+
{
11+
$this->callMany([
12+
UserSeeder::class,
13+
// SubmissionSeeder::class
14+
]);
15+
}
16+
}

database/Seeders/UserSeeder.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
3+
namespace LaraCore\Database\Seeders;
4+
5+
use LaraCore\Database\Factories\UserFactory;
6+
use LaraCore\Framework\Seeders\Seeder;
7+
8+
class UserSeeder extends Seeder
9+
{
10+
public function run()
11+
{
12+
// call the UserFactory to create a quiz
13+
UserFactory::new()->create();
14+
}
15+
}

0 commit comments

Comments
 (0)