Skip to content
This repository has been archived by the owner on Sep 27, 2022. It is now read-only.

Commit

Permalink
update dummy data seeder
Browse files Browse the repository at this point in the history
  • Loading branch information
alhoqbani committed Nov 9, 2017
1 parent 7c3e20c commit 1e67185
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 45 deletions.
6 changes: 4 additions & 2 deletions database/BaseSeeder.php
Expand Up @@ -9,11 +9,13 @@ class BaseSeeder extends \Phinx\Seed\AbstractSeed
* @var \Illuminate\Database\Eloquent\Factory
*/
protected $factory;
/** @var \Faker\Generator */
protected $faker;

protected function init()
{
$faker = Faker\Factory::create();
$this->factory = new \Illuminate\Database\Eloquent\Factory($faker);
$this->faker = Faker\Factory::create();
$this->factory = new \Illuminate\Database\Eloquent\Factory($this->faker);
$factories = glob(static::FACTORIES__PATH . '*.php');
foreach ($factories as $factory) {
/** @noinspection PhpIncludeInspection */
Expand Down
18 changes: 18 additions & 0 deletions database/factories/CommentFactory.php
@@ -0,0 +1,18 @@
<?php

use Conduit\Models\Article;
use Conduit\Models\Comment;
use Conduit\Models\User;

$this->factory->define(Comment::class, function (\Faker\Generator $faker) {
return [
'body' => $faker->sentences(rand(1,5), true),
'article_id' => function () {
return $this->factory->of(Article::class)->create()->id;
},
'user_id' => function () {
return $this->factory->of(User::class)->create()->id;
},
];
});

20 changes: 0 additions & 20 deletions database/seeds/ArticlesTableSeeder.php

This file was deleted.

54 changes: 54 additions & 0 deletions database/seeds/DataSeeder.php
@@ -0,0 +1,54 @@
<?php

use Conduit\Models\Article;
use Conduit\Models\Comment;
use Conduit\Models\Tag;
use Conduit\Models\User;

class DataSeeder extends BaseSeeder
{

protected $usersCount = 20;

protected $tags = ["test", "dragons", "training", "angular", "Development", "article", "markdown", "react",];

/**
* Run Method.
*
* Write your database seeder using this method.
*
* More information on writing seeders is available here:
* http://docs.phinx.org/en/latest/seeding.html
*/
public function run()
{
$users = $this->factory->of(User::class)->times($this->usersCount)->create();

$tags = collect($this->tags)->map(function ($tag) {
return Tag::create(['title' => $tag]);
});

$users->random($this->usersCount * 0.75)->each(function ($user) use ($tags) {
$this->factory->of(Article::class)->times(rand(1, 5))->create([
'user_id' => $user->id,
])->each(function (Article $article) use ($tags) {
$article->tags()->sync($tags->random()->pluck('id')->toArray());
});
});

$articles = Article::all();

$articles->each(function (Article $article) {
$this->factory->of(Comment::class)->times(rand(0, 5))->create(
['article_id' => $article->id, 'user_id' => $this->faker->numberBetween(1, $this->usersCount)]);
});

$articles->each(function (Article $article) use ($users) {
$article->favorites()->sync($users->random()->pluck('id')->toArray());
});

$users->each(function (User $user) use ($users) {
$user->followings()->sync($users->random(rand(0, 10))->pluck('id')->toArray());
});
}
}
23 changes: 0 additions & 23 deletions database/seeds/UsersTableSeeder.php

This file was deleted.

0 comments on commit 1e67185

Please sign in to comment.