:::: MENU ::::

Seeding Active Users in Laravel Spark

In case you didn’t know, Laravel has a package that provides scaffolding to get your next project up and running really quickly called Spark.

I’ve been using it for a while to build a little project that I’ll hopefully one day release.

Tonight I was working on trying to seed users to the database and have them already recognised as active users by Spark. I though this meant I’d be providing each seeded user with stripe information – a task I wasn’t looking forward to automating.

After Googling for a bit, jumping onto Laracasts forums and onto Slack (I was moments away from installing IRC), I discovered that I should just look at things for myself.

It turns out (unless I’m completely wrong) that all I had to do to seed active users for Laravel Spark was make a factory for the Subscription model that is part of Spark.

$factory->define(Laravel\Spark\Subscription::class, function (Faker\Generator $faker) {
    return [
        'user_id' => '1',
        'name' => 'default',
        'stripe_id' => '',
        'stripe_plan' => 'basic-yearly',
        'quantity' => '1',
        'trial_ends_at' => null,
        'ends_at' => null
    ];
});

After that, it was just a matter of throwing that factory into my DatabaseSeeder.php

factory(App\User::class, 25)
    ->create([
        'current_billing_plan' => 'basic-yearly'
    ])
    ->each(function ($user){
        factory(Laravel\Spark\Subscription::class)
            ->create(['user_id' => $user->id]);
});

So far it seems that I needn’t worry about the stripe_id columns, as it seems to work as is.

There is probably a better way to do this – if there is please let me know!


So, what do you think ?

You must be logged in to post a comment.