Skip to content

Instantly share code, notes, and snippets.

@dillinghamio
Last active November 23, 2020 13:07
Show Gist options
  • Star 19 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save dillinghamio/fd32bd49b0176a085dc088f3bd2d01c7 to your computer and use it in GitHub Desktop.
Save dillinghamio/fd32bd49b0176a085dc088f3bd2d01c7 to your computer and use it in GitHub Desktop.
Spark User Team Seeding

Seeding Users & Teams In Laravel Spark

Makes 5 users each with 1 team that has 5 members

Add a team factory to database/factories/ModelFactory.php

$factory->define(App\Team::class, function (Faker\Generator $faker) {
    return [
        'name' => $faker->sentence,
    ];
});

And use something like so within a seeder class

factory(App\User::class, 5) // team owners
    ->create()
    ->each(function($user) {
        
        factory(App\Team::class)
            ->create(['owner_id' => $user->id])
            ->each(function($team) use($user) {

                $team->users()->attach($user, ['role' => 'owner']);
                
                factory(App\User::class, 5) // team members
                    ->create()
                    ->each(function($user) use ($team) {

                        $team->users()->attach($user, ['role' => 'member']);

                    });
            });
@aManNamedJed
Copy link

Thanks a ton

@kamalkhan
Copy link

To add the trial days to the seed, append trial_ends_at to the team factory:

return [
    // ...,
    'trial_ends_at' => Carbon\Carbon::now()->addDays(Laravel\Spark\Spark::teamTrialDays()),
];

@cotton9
Copy link

cotton9 commented Mar 13, 2018

Not sure why, but w/ Spark 6.0, I had to specify how many teams I wanted the factory to create for each user:

factory(App\Team::class, 1)

// simply using factory(App\Team::class) resulted in 5 owners for team one, 4 owners for team two, etc.

@danpalmieri
Copy link

Thanks!

@shawnhooper
Copy link

Thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment