diff --git a/database/factories/CategoryFactory.php b/database/factories/CategoryFactory.php new file mode 100644 index 00000000..601b804b --- /dev/null +++ b/database/factories/CategoryFactory.php @@ -0,0 +1,27 @@ +define(\App\Models\Category::class, function (Faker $faker) { + + $user = \App\Models\User::first(); + + if (empty($user)) { + throw new Exception('Users need to be generated prior to generating categories.'); + } + + // Select a parent category with a propability of 30% + $has_parent = $faker->boolean(30); + + if ($has_parent && \App\Models\Category::parentOnly()->count() > 0) { + $parent_category = \App\Models\Category::parentOnly()->inRandomOrder()->first()->id; + } + + return [ + 'user_id' => $user->id, + 'name' => ucwords($faker->words(random_int(1, 2), true)), + 'description' => random_int(0, 1) ? $faker->sentences(random_int(1, 2), true) : null, + 'parent_category' => $parent_category ?? null, + 'is_private' => $faker->boolean(10), + ]; +}); diff --git a/database/factories/LinkFactory.php b/database/factories/LinkFactory.php new file mode 100644 index 00000000..49b07f1c --- /dev/null +++ b/database/factories/LinkFactory.php @@ -0,0 +1,27 @@ +define(\App\Models\Link::class, function (Faker $faker) { + + $user = \App\Models\User::first(); + + if (empty($user)) { + throw new Exception('Users need to be generated prior to generating links.'); + } + + // Select a category with a propability of 70% + $has_category = $faker->boolean(70); + if ($has_category && \App\Models\Category::count() > 0) { + $category = \App\Models\Category::inRandomOrder()->first()->id; + } + + return [ + 'user_id' => $user->id, + 'category_id' => $category ?? null, + 'url' => $faker->url, + 'title' => $faker->boolean(70) ? $faker->words(random_int(1, 5), true) : $faker->domainName, + 'description' => $faker->boolean(70) ? $faker->sentences(random_int(1, 3), true) : null, + 'is_private' => $faker->boolean(10), + ]; +}); diff --git a/database/factories/TagFactory.php b/database/factories/TagFactory.php new file mode 100644 index 00000000..ce74b596 --- /dev/null +++ b/database/factories/TagFactory.php @@ -0,0 +1,18 @@ +define(\App\Models\Tag::class, function (Faker $faker) { + + $user = \App\Models\User::first(); + + if (empty($user)) { + throw new Exception('Users need to be generated prior to generating tags.'); + } + + return [ + 'user_id' => $user->id, + 'name' => $faker->words(random_int(1, 3), true), + 'is_private' => $faker->boolean(10), + ]; +}); diff --git a/database/factories/UserFactory.php b/database/factories/UserFactory.php index facf2337..36d5344c 100644 --- a/database/factories/UserFactory.php +++ b/database/factories/UserFactory.php @@ -13,9 +13,9 @@ use Faker\Generator as Faker; | */ -$factory->define(App\User::class, function (Faker $faker) { +$factory->define(App\Models\User::class, function (Faker $faker) { return [ - 'name' => $faker->name, + 'name' => $faker->userName, 'email' => $faker->unique()->safeEmail, 'password' => '$2y$10$TKh8H1.PfQx37YgCzwiKb.KjNyWgaHb9cbcoQgdIVFlYg7B77UdFm', // secret 'remember_token' => str_random(10), diff --git a/database/seeds/ExampleSeeder.php b/database/seeds/ExampleSeeder.php new file mode 100644 index 00000000..54ac01ce --- /dev/null +++ b/database/seeds/ExampleSeeder.php @@ -0,0 +1,28 @@ +create(); + factory(\App\Models\Category::class, 10)->create(); + factory(\App\Models\Category::class, 10)->create(); // Generate some child categories + $tags = factory(\App\Models\Tag::class, 30)->create(); + + // Generate links and attach tags to them + factory(\App\Models\Link::class, 50)->create()->each(function (\App\Models\Link $link) use ($tags) { + if (random_int(0, 1)) { + // Attach a random number of tags to the link + $link->tags()->sync($tags->random(random_int(1, 30))->pluck('id')); + } + }); + } +}