mirror of
https://github.com/Kovah/LinkAce.git
synced 2025-01-17 21:28:30 +01:00
Create an example seeder with model factories
This commit is contained in:
parent
98d928bfb1
commit
d018174cdf
27
database/factories/CategoryFactory.php
Normal file
27
database/factories/CategoryFactory.php
Normal file
@ -0,0 +1,27 @@
|
||||
<?php
|
||||
|
||||
use Faker\Generator as Faker;
|
||||
|
||||
$factory->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),
|
||||
];
|
||||
});
|
27
database/factories/LinkFactory.php
Normal file
27
database/factories/LinkFactory.php
Normal file
@ -0,0 +1,27 @@
|
||||
<?php
|
||||
|
||||
use Faker\Generator as Faker;
|
||||
|
||||
$factory->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),
|
||||
];
|
||||
});
|
18
database/factories/TagFactory.php
Normal file
18
database/factories/TagFactory.php
Normal file
@ -0,0 +1,18 @@
|
||||
<?php
|
||||
|
||||
use Faker\Generator as Faker;
|
||||
|
||||
$factory->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),
|
||||
];
|
||||
});
|
@ -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),
|
||||
|
28
database/seeds/ExampleSeeder.php
Normal file
28
database/seeds/ExampleSeeder.php
Normal file
@ -0,0 +1,28 @@
|
||||
<?php
|
||||
|
||||
use Illuminate\Database\Seeder;
|
||||
|
||||
class ExampleSeeder extends Seeder
|
||||
{
|
||||
/**
|
||||
* Run the database seeds.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function run()
|
||||
{
|
||||
// Generate users, categories and tags
|
||||
factory(\App\Models\User::class, 1)->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'));
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user