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

jrdavidson's avatar

My Table Seeder Can Not Be Found

For some reason I don't understand why I'm receiving the error message:

[ReflectionException]
  Class UserProfileSocialLinksTableSeeder does not exist
<?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' => 'testing',
            'twitter_username' => 'testing',
            'google_username' => NULL,
            'behance_username' => NULL,
            'pinterest_username' => NULL,
            'linkedin_username' => 'testing',
            'github_username' => 'testing',
            'youtube_username' => 'testing',
            'instagram_username' => 'testing',
            'external_link' => 'NULL'
        ]);

        //TestDummy::create('App\UserProfileSocialLinks');
    }
0 likes
31 replies
jrdavidson's avatar

@usman I did and that helped however I'm getting hte Mass Assignment Exception and I know how to fix that by making it fillable or guarded. But its asking about the user_id field. So that would mean I'd have to make that a guarded field because all the other fields I want able to be filled. The user_id field will get set when the database table is put together and profiles are made. Correct?

bytefury's avatar

I dont understand what you are trying to say but if you dont want to set fillable property then Just call Eloquent::unguard(); before running seeders and it will run without giving you Mass Assignment Exception.

usman's avatar

@xtremer360 you don't have to do anything inside your models, just use the Model::unguard() method for temporary unlock in your calling seeder class.

<?php

use Illuminate\Database\Seeder;
use Illuminate\Database\Eloquent\Model;

class DatabaseSeeder extends Seeder {

    /**
     * Run the database seeds.
     *
     * @return void
     */
    public function run()
    {
        Model::unguard();

        $this->call('YourSeederClassName');
    }

}
1 like
jrdavidson's avatar

@usman I actually do run that command inside my Database Seeder.

<?php

use Illuminate\Database\Seeder;
use Illuminate\Database\Eloquent\Model;

class DatabaseSeeder extends Seeder {

    private $tables = [
        'roles',
        'permissions',
        'user_statuses',
        'users',
        'user_profiles',
        'user_profile_social_links',
        'permission_role',
        'role_user',
    ];

    private $seeders = [
        'RolesTableSeeder',
        'PermissionsTableSeeder',
        'UserStatusesTableSeeder',
        'UsersTableSeeder',
        'UserProfilesTableSeeder',
        'UserProfileSocialLinksTableSeeder',
        'PermissionRoleTableSeeder',
        'RoleUserTableSeeder',
    ];

    /**
     * Run the database seeds.
     *
     * @return void
     */
    public function run()
    {
        $this->cleanDatabase();

        Model::unguard();

        foreach ($this->seeders as $seeder)
        {
            $this->call($seeder);
        }

    }

    private function cleanDatabase()
    {
        DB::statement('SET FOREIGN_KEY_CHECKS=0');

        foreach ($this->tables as $tableName)
        {
            DB::table($tableName)->truncate();
        }

        DB::statement('SET FOREIGN_KEY_CHECKS=1');
    }

}

jrdavidson's avatar

@usman 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.

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.
<?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
]);


jrdavidson's avatar

@Ruffles You always good at providing me with clear responses that I've marked as answers. Could you assist on this one?

davorminchorov's avatar

I'd go for a simpler version of your tables, maybe put all of those social links into the user profile table instead of putting them into a separate table. The queries will be simpler. social links is a good enough name for a table, adding user profile makes the name too long. Either way works.

MThomas's avatar

@xtremer360 I think he means that you should merge your UserProfile and UserProfileSocialLinks to one mode/table.

1 like
davorminchorov's avatar

Yup, that's what I mean. Social Links are part of the user profile. But you can go with your way too, just shorten the name a little bit if you choose to go that way (social links instead of user profile social links).

jrdavidson's avatar

@Ruffles non an you help me fix my actual issue because it's not seeding my profiles table correctly. It's adding me and the last dummy user only.

davorminchorov's avatar

So you get 2 rows instead of 200? Check the TestDummy call and see if you've specified the number of profile data. According to the examples above, you only have 200 specified for users but not user profiles.

jrdavidson's avatar

@Ruffles Right. Actually I get 202 total entries into the users table. 1 is for me as there's 201 entries test dummy users. So I'm not quite sure why it shows a total of 202 total rows in the user's table. What do you think the code should look like then?

davorminchorov's avatar

Try lowering the number of times on the Test Dummy and see if you'll get more than 200. If it's that, you are creating an extra user for some reason somewhere / somehow.

jrdavidson's avatar

@Ruffles I say this because when I run the profile table seeder it shows the data like this:

id   user_id  
1        1           
2      202
3      202
4      202
5        ... on so on.
davorminchorov's avatar

Hmmm strange. Try adding times(200) to the user profile Test dummy to see the difference.

jrdavidson's avatar

@Ruffles I have no idea why its doing this but when I comment out the seeders AFTER the UsersTableSeeder then it creates me and 200 as desired. As I can tell by having me in the database and the last row having a user_id of 201. But if I uncomment the UserProfilesTableSeeder then when it reseeds everything and the users table has me and the last row as 202 as the id.

davorminchorov's avatar

Try changing the order of the table seeder calls then.

private $seeders = [
        'UsersTableSeeder',
        'RolesTableSeeder',
        'PermissionsTableSeeder',
        'RoleUserTableSeeder',
        'PermissionRoleTableSeeder',
        'UserStatusesTableSeeder',
        'UserProfilesTableSeeder',
        'UserProfileSocialLinksTableSeeder',
    ];

jrdavidson's avatar

@Ruffles I did that and still received the same results. Is there a way that I can have it echo out to me the query of the seed file when its finished running. It might help with debugging as to why its doing this?

davorminchorov's avatar

You can put it anywhere, if it's for debugging only. If you wanna keep it, create an event class or a service provider.

Also, you can log stuff.

jrdavidson's avatar
<?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');
        Event::listen();
    }

}

I did this and its asking me to put in an argument 1 and I"m not sure what I need to place there.

jrdavidson's avatar

@Ruffles I removed the Event::listen() line AND commented out the TestDummy line as well. Then reran my seeder file. I now have two records in my table. One account for me and another dummy account.

Could this now have something to do with how I coded my factories file?

jrdavidson's avatar

@Ruffles I figured out why I kept getting a second user inserted into my db.

<?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',  ---------------------------> Was the reason why. Not sure how to fix though.
    '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

@Ruffles Forgetting about profiles for the moment does that have something to do with it putting in the wrong amount of users?

Next

Please or to participate in this conversation.