/** * Run the database seeds. * * @return void */ public function run() { factory(App\User::class, 50)->create()->each(function ($user) { // Each user will create 5 posts for ($i = 0; $i < 5; $i++) { $user->posts()->save(factory(App\Post::class)->make()); } // Each user will favorite 2 posts for ($i = 0; $i < 2; $i++) { // Store a random post_id $temp = rand(1, App\Post::all()->count()); // If the loop has not run before assigned a value to $lastTemp if ($i == 0) { $lastTemp = 0; } // To prevent overlap, keep generating a new random post_id until a new post_id is generated while ($temp == $lastTemp) { $temp = rand(1, App\Post::all()->count()); } // Now that a new post_id has been generated, favorite the post $user->likes()->attach($temp); // Store the post_id to be rechecked in case the loop runs again $lastTemp = $temp; } }); }
/** * Run the database seeds. * * @return void */ public function run() { // $this->call(UserTableSeeder::class); factory(App\User::class, 50)->create()->each(function ($user) { $user->posts()->save(factory(App\Post::class)->make()); $post = App\Post::find(rand(1, App\Post::all()->count())); $user->userPosts()->attach($post); }); }
/** * Run the database seeds. * * @return void */ public function run() { factory(App\User::class, 50)->create()->each(function ($user) { $user->subbreddits()->save(factory(App\Subbreddit::class)->make()); $user->posts()->save(factory(App\Post::class)->make(['subbreddit_id' => rand(1, App\Subbreddit::all()->count())])); $user->comments()->save(factory(App\Comment::class)->make(['post_id' => rand(1, App\Post::all()->count())])); $user->comments()->save(factory(App\Comment::class)->make(['comment_id' => rand(1, App\Comment::all()->count())])); $user->subscribedSubbreddits()->attach(rand(1, App\Subbreddit::all()->count())); }); }
/** * Run the database seeds. * * @return void */ public function run() { // $this->call(UserTableSeeder::class); // change # to 10 for now factory(App\User::class, 10)->create()->each(function ($user) { $user->subbreddits()->save(factory(App\Subbreddit::class)->make()); $user->posts()->save(factory(App\Post::class)->make(['subbreddit_id' => rand(1, App\Subbreddit::all()->count())])); $user->comments()->save(factory(App\Comment::class)->make(['post_id' => rand(1, App\Post::all()->count())])); $user->comments()->save(factory(App\Comment::class)->make(['comment_id' => rand(1, App\Comment::all()->count())])); $user->subscribedSubbreddits()->attach(rand(1, App\Subbreddit::all()->count())); }); }
public function run() { // Uncomment the below to wipe the table clean before populating DB::table('comments')->delete(); $posts = App\Post::all(); $users = App\User::all(); for ($i = 1; $i <= 3; $i++) { $comments = array(['body' => "Nice post!", 'user_id' => $i, 'commentable_id' => $i * 8 % 11 + 1, 'commentable_type' => 'App\\Post', 'created_at' => new DateTime(), 'updated_at' => new DateTime()], ['body' => "Agreed.", 'user_id' => $i, 'commentable_id' => $i * 8 % 11 + 2, 'commentable_type' => 'App\\Post', 'created_at' => new DateTime(), 'updated_at' => new DateTime()], ['body' => "No way!", 'user_id' => $i, 'commentable_id' => $i * 8 % 11 + 3, 'commentable_type' => 'App\\Post', 'created_at' => new DateTime(), 'updated_at' => new DateTime()], ['body' => "I guess...", 'user_id' => $i, 'commentable_id' => $i * 8 % 11 + 4, 'commentable_type' => 'App\\Post', 'created_at' => new DateTime(), 'updated_at' => new DateTime()]); // Uncomment the below to run the seeder DB::table('comments')->insert($comments); } }
/** * Run the database seeds. * * @return void */ public function run() { //factory(PostHasTag::class, 50)->create(); $posts = App\Post::all(); $tags = App\Tag::all(); foreach ($posts as $post) { $list = []; foreach ($tags as $tag) { rand(0, 5) == 0 && count($list) < 10 ? $list[] = $tag->id : null; } $post->tags()->sync($list); } }
/** * Run the database seeds. * * @return void */ public function run() { factory(App\User::class, 50)->create()->each(function ($user) { // Each user will make 1 subbreddit $user->subbreddits()->save(factory(App\Subbreddit::class)->make()); // Each user will make 1 post on a random subbreddit $user->posts()->save(factory(App\Post::class)->make(['subbreddit_id' => rand(1, App\Subbreddit::all()->count())])); // Each user will leave a comment on a random post $user->comments()->save(factory(App\Comment::class)->make(['post_id' => rand(1, App\Post::all()->count())])); // Each user will leave a comment on a random comment $user->comments()->save(factory(App\Comment::class)->make(['comment_id' => rand(1, App\Comment::all()->count())])); // Each user will subscribe to 1 subbreddit $user->subscribedSubbreddits()->attach(rand(1, App\Subbreddit::all()->count())); }); }
<?php /* |-------------------------------------------------------------------------- | Model Factories |-------------------------------------------------------------------------- | | Here you may define all of your model factories. Model factories give | you a convenient way to create models for testing and seeding your | database. Just tell the factory how a default model should look. | */ // User Factory $factory->define(App\User::class, function (Faker\Generator $faker) { return ['name' => $faker->name, 'email' => $faker->email, 'password' => bcrypt(str_random(10)), 'remember_token' => str_random(10)]; }); // Post Factory $factory->define(App\Post::class, function (Faker\Generator $faker) { return ['title' => $faker->sentence(), 'short_story' => $faker->text(500), 'full_story' => $faker->text(2000), 'image' => $faker->imageUrl($width = 640, $height = 480), 'user_id' => App\User::all()->random(1)->id]; }); // Comment Factory $factory->define(App\Comment::class, function (Faker\Generator $faker) { return ['comment' => $faker->text(rand(100, 1000)), 'post_id' => App\Post::all()->random(1)->id, 'user_id' => App\User::all()->random(1)->id]; });
<?php /* |-------------------------------------------------------------------------- | Model Factories |-------------------------------------------------------------------------- | | Here you may define all of your model factories. Model factories give | you a convenient way to create models for testing and seeding your | database. Just tell the factory how a default model should look. | */ $factory->define(App\User::class, function (Faker\Generator $faker) { return ['username' => $faker->userName, 'email' => $faker->email, 'password' => bcrypt(str_random(10)), 'remember_token' => str_random(10)]; }); $factory->define(App\Category::class, function (Faker\Generator $faker) { return ['parent_id' => mt_rand(null, 5), 'name' => $faker->words(3, true), 'description' => $faker->words(5, true)]; }); $factory->define(App\Post::class, function (Faker\Generator $faker) { return ['category_id' => mt_rand(1, 100), 'user_id' => mt_rand(1, 10), 'title' => $faker->words(4, true), 'content' => join("\n\n", $faker->paragraphs(mt_rand(6, 20))), 'created_at' => $faker->dateTimeBetween('-1 month', '+3 days')]; }); $factory->define(App\PostImage::class, function (Faker\Generator $faker) { return ['post_id' => $faker->randomElement(App\Post::all()->lists('id')->toArray()), 'path' => $faker->imageUrl($width = 640, $height = 480, 'cats', true)]; }); $factory->define(App\State::class, function (Faker\Generator $faker) { return ['state' => $faker->state, 'state_abbr' => $faker->stateAbbr]; }); $factory->define(App\City::class, function (Faker\Generator $faker) { return ['state_id' => mt_rand(1, 5), 'city' => $faker->city]; });
<?php /* |-------------------------------------------------------------------------- | Model Factories |-------------------------------------------------------------------------- | | Here you may define all of your model factories. Model factories give | you a convenient way to create models for testing and seeding your | database. Just tell the factory how a default model should look. | */ $factory->define('App\\User', function ($faker) { return ['name' => $faker->name, 'email' => $faker->email, 'password' => bcrypt(str_random(10)), 'remember_token' => str_random(10)]; }); $factory->define('App\\Post', function ($faker) { return ['title' => $faker->sentence, 'content' => $faker->paragraph]; }); $factory->define('App\\Tag', function ($faker) { return ['name' => $faker->word]; }); $factory->define('App\\Comment', function ($faker) { return ['name' => $faker->word, 'post_id' => App\Post::all()->random()->id, 'comment' => $faker->sentence, 'email' => $faker->email, 'name' => $faker->name]; });
| Model Factories |-------------------------------------------------------------------------- | | Here you may define all of your model factories. Model factories give | you a convenient way to create models for testing and seeding your | database. Just tell the factory how a default model should look. | */ $factory->define(App\User::class, function ($faker) { $firs_name = $faker->firstName; $last_name = $faker->lastName; return ['identification' => $faker->unique()->numberBetween(5000000, 39999999), 'first_name' => $firs_name, 'last_name' => $last_name, 'full_name' => "{$firs_name} {$last_name}", 'type' => 'user', 'email' => $faker->unique()->email, 'password' => '123456', 'remember_token' => str_random(10)]; }); $factory->define(App\Category::class, function ($faker) { return ['title' => $faker->sentences(5), 'description' => $faker->paragraph(1)]; }); $factory->define(App\Post::class, function ($faker) { return ['highlight' => rand(0, 1), 'title' => $faker->sentence(5), 'description' => $faker->paragraph(5), 'content' => $faker->paragraph(10), 'image' => $faker->imageUrl(1140, 400), 'user_id' => App\User::all()->random()->id, 'category_id' => App\Category::all()->random()->id]; }); $factory->define(App\Comment::class, function ($faker) { return ['content' => $faker->paragraph(1), 'user_id' => App\User::all()->random()->id, 'post_id' => App\Post::all()->random()->id]; }); $factory->define(App\Reply::class, function ($faker) { return ['content' => $faker->paragraph(1), 'user_id' => App\User::all()->random()->id, 'comment_id' => App\Comment::all()->random()->id]; }); $factory->define(App\Application::class, function ($faker) { return ['status' => $faker->randomElement(['process', 'accepted', 'rejected']), 'message' => $faker->paragraph(10), 'user_id' => App\User::all()->random()->id]; }); $factory->define(App\History::class, function ($faker) { return ['status' => $faker->randomElement(['process', 'accepted', 'rejected']), 'message' => $faker->paragraph(10), 'user_id' => App\User::all()->random()->id, 'application_id' => App\Application::all()->random()->id]; });
<?php /* |-------------------------------------------------------------------------- | Model Factories |-------------------------------------------------------------------------- | | Here you may define all of your model factories. Model factories give | you a convenient way to create models for testing and seeding your | database. Just tell the factory how a default model should look. | */ $factory->define(App\User::class, function (Faker\Generator $faker) { return ['name' => $faker->name, 'email' => $faker->email, 'password' => bcrypt(str_random(10)), 'remember_token' => str_random(10)]; }); $factory->define(App\Post::class, function (Faker\Generator $faker) { return ['title' => $faker->sentence, 'resume' => $faker->paragraph, 'text' => $faker->realText(2000)]; }); $factory->define(App\Tag::class, function (Faker\Generator $faker) { return ['name' => $faker->word]; }); $factory->define(App\Comment::class, function (Faker\Generator $faker) { $posts = App\Post::all()->lists('id'); return ['post_id' => $faker->randomElement($posts->all()), 'comment' => $faker->paragraph, 'name' => $faker->name, 'email' => $faker->email]; }); $factory->define(App\PostHasTag::class, function (Faker\Generator $faker) { $posts = App\Post::all()->lists('id'); $tags = App\Tag::all()->lists('id'); return ['post_id' => $faker->randomElement($posts->all()), 'tag_id' => $faker->randomElement($tags->all())]; });