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

zelaza's avatar

How to Register/Create a User Programmatically?

Hi. I need to register/create a User programmatically, without going through the normal, interactive, form-based registration process.

I thought about mimicking what's in the register() method in the RegisterController:

    public function register(RegisterRequest $request)
    {
        Auth::login($user = Spark::interact(
            Register::class, [$request]
        ));

        event(new UserRegistered($user));

        return response()->json([
            'redirect' => $this->redirectPath()
        ]);
    }

but I don't know how to mimic the $request argument to Spark::interact().

So then I thought about trying to mimic the registration form post request to the RegisterController@register() method thus:

    public function register_user()
    {

        $data = [
            'stripe_token' => '',
            'plan' => '',
            'team' => '',
            'team_slug' => '',
            'name' => 'Joe User',
            'email' => '[email protected]',
            'password' => 'j0eybaby',
            'password_confirmation' => 'j0eybaby',
            'address' => '',
            'address_line_2' => '',
            'city' => '',
            'state' => '',
            'zip' => '',
            'country' => 'US',
            'vat_id' => '',
            'terms' => true,
            'coupon' => null,
            'invitation' => null,
            'errors' => array('errors' => (object) array()),
            'busy' => true,
            'successful' => false,
        ];

        $client = new  GuzzleHttp\Client([
            'headers' =>
                [
                    'Content-Type' => 'application/json;charset=UTF-8',
                    'X-Csrf-Token' => csrf_token(),
                    'X-Requested-With' => 'XMLHttpRequest',
                ]
        ]);

        // issue http post request that mimics registration form post request...
        $client->post('https://www.mysite.com/register', [ 'body' => json_encode($data) ]);

    }

but that gives me a 500 Internal Server error - not sure why, maybe I'm not sending some cookies that I need to send...

Is there any way to do what I'm trying to do?

Thanks.

0 likes
6 replies
zelaza's avatar

No ideas on how to do this...?

bencarter78@hotmail.com's avatar

Can't you do something like...

User::create([
    'name' => 'Joe User',
    'email' => '[email protected]',
    'password' => 'j0eybaby',
    'password_confirmation' => 'j0eybaby',
    'terms' => true,
    'busy' => true,
    'successful' => false
]);



event(new UserRegistered($user));

Maybe put this in a console command depending on your use case?

zelaza's avatar

Thanks for the response, Ben. I actually started thinking about something like that, but I'm thinking that since this is Spark and not just straight Laravel I can't just create a User. I think Spark does a lot more stuff when it creates a User...

themsaid's avatar

Hello there,

It depends on your user case really, like if you simply want to create a new Spark user that's on a generic trial you can simply use Eloquent's create() method as you'd do with any Model, that's actually how spark does it:

    $user = Spark::user();

    $user->forceFill([
        'name' => $data['name'],
        'email' => $data['email'],
        'password' => bcrypt($data['password']),
        'last_read_announcements_at' => Carbon::now(),
        'trial_ends_at' => Carbon::now()->addDays(Spark::trialDays()),
    ])->save();

    return $user;
4 likes
zelaza's avatar

Thanks very much, themsaid. That looks like what I need. I will try it!

EventFellows's avatar

I suggest you follow the request that spark normaly does (it is 4-5 steps) from the register() method you mention to the forceFill() that @themsaid mentions.

You can 'hook in/ mimic' at any of those steps depending on what you need to do for your use case.

Please or to participate in this conversation.