Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

jrdavidson's avatar

TestDummy

The following UsersTableSeeder works fine however when it gets to the UsersProfileTableSeeder then it becomes an issue.

<?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' => 'Test',
            'last_name' => 'Testing',
            'username' => 'testing',
            'email' => 'testing@gmail.com',
            'password' => 'testing',
        ]);

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

}

When it gets to this point it will create my profile and an create an additional extra profile for me as dummy data for some reason. Does anyone know how for me to recode this so this does not happen.

<?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' => 'test.jpg,
            'bio' => 'This is just my personal biography!',
            'address' => '1234 West My Street',
            'city' => 'Testing',
            'state' => 'TX',
            'postcode' => '12345',
            'country' => 'United States',
            'phone' => '913-387-7359',
            'birthday' => '1984-12-18'
        ]);

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

}
0 likes
28 replies
bobbybouwmann's avatar

I don't think I completely understand your question. If you have a relation setup between the User and the profile then it will create both of them. Can you post your factories.php file?

jrdavidson's avatar

@blackbird

<?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' => NULL,
    '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
]);
bobbybouwmann's avatar

So what is your question exactly? So say it's creating 202 profiles or 202 users?

jrdavidson's avatar

Well as of right now when I run the user's seeder it creates me and then for some reason it also creates 201 dummy users and I don't know why.

bobbybouwmann's avatar

It's because you have a relation setup with App\User on this part 'user_id' => 'factory:App\User'. If you don't override the relation testdummy will create a user object for you, because profile is depending on it

jrdavidson's avatar

How should I go about adjusting this so that it doesn't do this?

bobbybouwmann's avatar

You can override variables really easy

TestDummy::times(200)->create('App\UserProfile', ['user_id' => '2');

Now it doesn't create a relation anymore since you specify one already

jrdavidson's avatar

@blackbird

I'm trying to deal with the user's table right now though not the profiles.

I try and run the following but it says::

  [Illuminate\Database\QueryException]
  SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry '2' for key 'PRIMARY' ...
<?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' => 'Jeffrey',
            'last_name' => 'Davidson',
            'username' => 'xtremer360',
            'email' => 'xtremer360@gmail.com',
            'password' => 'Disney14',
        ]);

        TestDummy::times(200)->create('App\User', ['id' => '2']); -----> won't let me do this
    }

}
jrdavidson's avatar

I'm still trying to understand why I can't get the right amount of users inserted in to my database.

davorminchorov's avatar

Run TestDummy::times(200)->create('App\User'); first, and then create your own user. See if that changes anything.

jrdavidson's avatar

This works correctly.

User::create([
            'first_name' => 'Test',
            'last_name' => 'Test',
            'username' => 'test',
            'email' => 'test@gmail.com',
            'password' => 'testing',
        ]);

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

And this works correctly.


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

User::create([
            'first_name' => 'Test',
            'last_name' => 'Test',
            'username' => 'test',
            'email' => 'test@gmail.com',
            'password' => 'testing',
        ]); 

For some reason when my DatabaseSeeder.php file gets to the next seeder which is UserProfileTableSeeder it for some reason adds an additional row to my user's table.

<?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' => 'secret.jpg',
            'bio' => 'This is just my personal biography!',
            'address' => '1234 Fake Street',
            'city' => 'Secret',
            'state' => 'Secret',
            'postcode' => '12345',
            'country' => 'United States',
            'phone' => '123-456-7890',
            'birthday' => '1984-12-18'
        ]);

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

}
<?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',  <----not an increment field. It is a foreign key to the user's table.
    'bio' => $faker->sentence(100),
    'avatar' => NULL,
    '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')
]);
jrdavidson's avatar

So I don't know why when I get to the point now of UserProfiles it decides to go in and make another user account.

jrdavidson's avatar

Is there anyone else who might understand why this is happening and how to correct?

impbob36's avatar

Your UserProfiles factory contains a relationship:

$factory('App\UserProfile', [
    'user_id' => 'factory:App\User',  <----not an increment field. It is a foreign key to the user's table.
...

So when you call it, it's automatically creating an associated relationship as well.

See the relationship section: https://github.com/laracasts/TestDummy

jrdavidson's avatar

Thank you for the response. How should I refactor this then.

jrdavidson's avatar

Can someone suggest to me what they think I should do to avoid this situation?

jrdavidson's avatar

Still looking for some suggestions on best ways to handle this?

jrdavidson's avatar

Is there nobody that has had to deal with TestDummy and can help me see what I'm doing wrong here?

davorminchorov's avatar

I don't know how to help you with this, but I can give you an alternative option. Use faker directly in the migration files and try to populate the tables like that. There are a few videos on how to do it here on Laracasts.

jrdavidson's avatar

Yeah I am not getting anywhere with TestDummy so I'm tossing it out. I'm going to continue the conversation in the other thread about users and roles.

impbob36's avatar

So what exactly are you trying to do?

Based on your previous factories (listed at bottom)

If you want 200 'App\User':

Factory::times(200)->create('App\User')

If you want 200 'App\Users' with 'App\UserProfile'

Factory::times(200)->create('App\UserProfile') /// <-- This factory has a relationship, so it will generate 'App\User'

If you want 200 'App\Users' with 'App\UserProfile' and 'App\UserProfileSocialLinks'

Factory::times(200)->create('App\UserProfileSocialLinks') /// <-- This factory has a relationship, so it will generate 'App\UserProfile', which has a relationship will generate a 'App\User'.

Original factories

$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' => NULL,
    '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
]);
jrdavidson's avatar

@impbob Oh wow. Thank you. However its not completely working.

  [Illuminate\Database\QueryException]
  SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry '2' for key 'user_profile_social_links_user_id_unique' (SQL: insert into `user_profile_social_links` (`user_id`, `facebook_username`, `twitter_username`, `google_username`, `behance_username`, `pinterest_u
  sername`, `linkedin_username`, `github_username`, `youtube_username`, `instagram_username`, `updated_at`, `created_at`) values (2, larkin.otho, freeman27, harrison45, trycia99, max15, aziemann, rtillman, hilma.macejkovic, fcarroll, 2015-04-03 03:42:27, 2015-04-03 03:42:27))

  [PDOException]
  SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry '2' for key 'user_profile_social_links_user_id_unique'
davorminchorov's avatar

set them to be unique by adding unique before the type of the field like:

'facebook_username' => $faker->optional($weight = 0.9)->unique()->userName // check the faker docs how to use unique.
jrdavidson's avatar

@Ruffles

I fixed all the ones that said it would be unique but its saying in the error about the user_id field needing to be unique() which is true but with how my factories file looks I don't know how to account for the user_id in the social links factory right now.

Please or to participate in this conversation.