Add factory for Tag model, create 3 tags per user in UsersTableSeeder and get rid of TagsTableSeeder
MODIFY
database/factories/ModelFactory.php
database/factories/ModelFactory.php
use Faker\Generator;
use App\User;
use App\Space;
+use App\Tag;
use App\Earning;
use App\Spending;
@@ -22,6 +23,12 @@ $factory->define(Space::class, function (Generator $faker) {
];
});
+$factory->define(Tag::class, function (Generator $faker) {
+ return [
+ 'name' => ucfirst($faker->word)
+ ];
+});
+
$factory->define(Earning::class, function (Generator $faker) {
return [
'happened_on' => $faker->dateTimeBetween('-50 days', 'now')->format('Y-m-d'),

MODIFY
database/seeds/DatabaseSeeder.php
database/seeds/DatabaseSeeder.php
@@ -6,6 +6,5 @@ class DatabaseSeeder extends Seeder {
public function run() {
$this->call(CurrenciesTableSeeder::class);
$this->call(UsersTableSeeder::class);
- $this->call(TagsTableSeeder::class);
}
}

DELETE
database/seeds/TagsTableSeeder.php
-<?php
-
-use Illuminate\Database\Seeder;
-
-use App\Tag;
-
-class TagsTableSeeder extends Seeder {
- public function run() {
- $names = [
- 'Groceries',
- 'Bills',
- 'Transport'
- ];
-
- foreach ($names as $name) {
- $currentTimestamp = date('Y-m-d H:i:s');
-
- Tag::insert([
- 'space_id' => 1,
- 'name' => $name,
- 'created_at' => $currentTimestamp,
- 'updated_at' => $currentTimestamp
- ]);
- }
- }
-}

MODIFY
database/seeds/UsersTableSeeder.php
database/seeds/UsersTableSeeder.php
@@ -4,13 +4,20 @@ use Illuminate\Database\Seeder;
use App\User;
use App\Space;
+use App\Tag;
class UsersTableSeeder extends Seeder {
public function run() {
factory(User::class, 5)->create()->each(function ($user) {
- $user->spaces()->attach(factory(Space::class)->create([
+ $space_id = factory(Space::class)->create([
'name' => $user->name . '\'s Space'
- ]));
+ ]);
+
+ $user->spaces()->attach($space_id);
+
+ factory(Tag::class, 3)->create([
+ 'space_id' => $space_id
+ ]);
});
}
}

Add factory for Tag model, create 3 tags per user in UsersTableSeeder and get rid of TagsTableSeeder