Attempt At Seeding With Faker and TestDummy For Ef

2019-08-10 06:30发布

I'm wondering if I need to rethink how I'd seeding all my tables with my seeders. Is there a better more efficient way of handling all this data. After run my seed classes for some reason I have a total of 202 users. One is me and then 201 dummy users but there should be only 200 dummy users. I don't know where the extra user is getting made at.

Goals:

  1. Create a user account only for me with supplied values for each field in the users table.
  2. Create 200 additional uses in the database with dummy data with testdummy & faker.
  3. Create a row for myself AND each of the other users to have a profile with supplied values for each field in the users profile table.
  4. Create a row for myself AND each of the other users to have links to social networking pages with supplied values for each field in the users profile social links table.

My Code Files

<?php

use Illuminate\Database\Seeder;

use Laracasts\TestDummy\Factory as TestDummy;
use App\User;

class UsersTableSeeder extends Seeder {

    public function run()
    {
        User::create([
            'first_name' => 'Secret',
            'last_name' => 'Secret',
            'username' => 'secret',
            'email' => 'secret@gmail.com',
            'password' => 'secret',
        ]);

        TestDummy::times(200)->create('App\User');
    }

}

<?php

use Illuminate\Database\Seeder;

use Laracasts\TestDummy\Factory as TestDummy;
use App\UserProfile;

class UserProfilesTableSeeder extends Seeder {

    public function run()
    {
        UserProfile::create([
            'user_id' => '1',
            'avatar' => NULL
            'bio' => 'This is just my personal biography!',
            'address' => NULL
            'city' => NULL
            'state' => NULL
            'postcode' => NULL
            'country' => NULL
            'phone' => NULL
            'birthday' => NULL
        ]);

        TestDummy::create('App\UserProfile');
    }

}

<?php

use Illuminate\Database\Seeder;

use Laracasts\TestDummy\Factory as TestDummy;
use App\UserProfileSocialLinks;

class UserProfileSocialLinksTableSeeder extends Seeder {

    public function run()
    {
        UserProfileSocialLinks::create([
            'user_id' => '1',
            'facebook_username' => NULL
            'twitter_username' => NULL
            'google_username' => NULL,
            'behance_username' => NULL,
            'pinterest_username' => NULL,
            'linkedin_username' => NULL
            'github_username' => NULL
            'youtube_username' => NULL
            'instagram_username' => NULL
            'external_link' => NULL
        ]);

        //TestDummy::create('App\UserProfileSocialLinks');
    }

}

<?php

$factory('App\User', [
    'first_name' => $faker->firstName,
    'last_name' => $faker->lastName,
    'username' => $faker->userName,
    'email' => $faker->email,
    'password' => $faker->word
]);

$factory('App\UserProfile', [
    'user_id' => 'factory:App\User',
    'bio' => $faker->sentence(100),
    'avatar' => $faker->imageUrl($width = 640, $height = 480),
    'address' => $faker->optional($weight = 0.9)->address,
    'city' => $faker->optional($weight = 0.9)->city,
    'state' => $faker->optional($weight = 0.9)->state,
    'postcode' => $faker->optional($weight = 0.9)->postcode,
    'country' => $faker->optional($weight = 0.9)->country,
    'phone' => $faker->optional($weight = 0.9)->phoneNumber,
    'birthday' => $faker->optional($weight = 0.9)->dateTimeBetween('-40 years', '-18 years')
]);

$factory('App\UserProfileSocialLinks', [
    'user_id' => 'factory:App\UserProfile',
    'facebook_username' => $faker->optional($weight = 0.9)->userName,
    'twitter_username' => $faker->optional($weight = 0.9)->userName,
    'google_username' => $faker->optional($weight = 0.9)->userName,
    'behance_username' => $faker->optional($weight = 0.9)->userName,
    'pinterest_username' => $faker->optional($weight = 0.9)->userName,
    'linkedin_username' => $faker->optional($weight = 0.9)->userName,
    'github_username' => $faker->optional($weight = 0.9)->userName,
    'youtube_username' => $faker->optional($weight = 0.9)->userName,
    'instagram_username' => $faker->optional($weight = 0.9)->userName
]);

0条回答
登录 后发表回答