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

alanstorm's avatar

Laravel Spark, Swap/Interact, and Private Variables

First post here, so apologies if I somehow violate community norms.

I'm using Laravel Spark. I want to swap in a new implementation for the configureTeamForNewUser method. At first, it looks like this is possible because of the Spark::interact call here

#File: spark/src/Interactions/Auth/Register.php
Spark::interact(self::class.'@configureTeamForNewUser', [$request, $user]);

i.e. the framework calls configureTeamForNewUser using Spark::interact, which means I can Spark::swap it.

However, if I look at the configureTeamForNewUser method itself

#File: spark/src/Interactions/Auth/Register.php
public function configureTeamForNewUser(RegisterRequest $request, $user)
{
    if ($invitation = $request->invitation()) {
        Spark::interact(AddTeamMember::class, [$invitation->team, $user]);

        self::$team = $invitation->team;

        $invitation->delete();
    } elseif (Spark::onlyTeamPlans()) {
        self::$team = Spark::interact(CreateTeam::class, [
            $user, ['name' => $request->team, 'slug' => $request->team_slug]
        ]);
    }

    $user->currentTeam();
}

This method assigns a value to the private $team class property. It's my understanding that if I use Spark::swap my callback is called instead of the original method. Initial tests confirm this. However, since my callback can't set $team, this means my callback would change the behavior of the system in a way that's going to break other spark functionality.

Is the above a correct understanding of the system? Or am I missing something, and it would be possible to swap in another function call (somehow calling the original configureTeamForNewUser)?

I'm using configureTeamForNewUser as a specific example of the more general problem. While help on the specific problem is appreciated, help with the more general problem is what I'm really after.

0 likes
4 replies
Cronix's avatar

I basically just copy the entire class, just for that reason, and don't use interact or swap if I can help it.

Example:

copy Laravel\Spark\Interactions\Settings\Profile\UpdateContactInformation.php

to App\SparkExtensions\UpdateContactInformation.php

In App\SparkExtensions\UpdateContactInformation.php, Add:

use Laravel\Spark\Interactions\Settings\Profile\UpdateContactInformation as SparkUpdateContactInformation;

Then change the class definition to: class UpdateContactInformation extends SparkUpdateContactInformation

Then in the SparkServiceProvide, add the new singleton to the register method:

public function register()
{
    $this->app->singleton(
       'Laravel\Spark\Contracts\Interactions\Settings\Profile\UpdateContactInformation',
            'App\SparkExtensions\UpdateContactInformation'
        );
}

Now you can alter the code in your new class and it will still work the same, and it won't affect the original spark files when updating since you didn't touch them.

9 likes
alanstorm's avatar

Thanks @Cronix -- that's definitely a great workaround! I'd still be curious to hear from others (particularly and folks who actually work at/on Laravel) is there's a way to use swap safely in these scenarios, or if I'm missing something.

KrishanK's avatar

I was looking for using Spark::swap to swap out an entire class. Unfortunately thats not possible with swap method.

@Cronix thanks a lot for your solution.

scottybowl's avatar

Don't forget to change the namespace at the top of the new file otherwise you'll get an error saying the Class already exists.

namespace App\SparkExtensions;

@Cronix - thank you for this solution!

Please or to participate in this conversation.