Skip to content

Commit 0fbd56d

Browse files
Prepupulate users and questions tables using model factories dan seeds
1 parent 7e55806 commit 0fbd56d

File tree

4 files changed

+27
-8
lines changed

4 files changed

+27
-8
lines changed

app/Question.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,10 @@ public function user()
1212
{
1313
return $this->belongsTo(User::class);
1414
}
15+
16+
public function setTitleAttribute($value)
17+
{
18+
$this->attributes['title'] = $value;
19+
$this->attributes['slug'] = str_slug($value);
20+
}
1521
}

app/User.php

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,4 @@ public function questions()
3131
{
3232
return $this->hasMany(Question::class);
3333
}
34-
35-
public function setTitleAttribute($value)
36-
{
37-
$this->attributes['title'] = $value;
38-
$this->attributes['slug'] = str_slug($value);
39-
}
40-
4134
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
3+
use Faker\Generator as Faker;
4+
5+
$factory->define(App\Question::class, function (Faker $faker) {
6+
return [
7+
'title' => rtrim($faker->sentence(rand(5, 10)), "."),
8+
'body' => $faker->paragraphs(rand(3, 7), true),
9+
'views' => rand(0, 10),
10+
'answers' => rand(0, 10),
11+
'votes' => rand(-3, 10)
12+
];
13+
});

database/seeds/DatabaseSeeder.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,13 @@ class DatabaseSeeder extends Seeder
1111
*/
1212
public function run()
1313
{
14-
// $this->call(UsersTableSeeder::class);
14+
factory(App\User::class, 3)->create()->each(function($u) {
15+
$u->questions()
16+
->saveMany(
17+
factory(App\Question::class, rand(1, 5))
18+
->make()
19+
);
20+
21+
});
1522
}
1623
}

0 commit comments

Comments
 (0)