Hi Emilio,
To allow users to own multiple teams and manage multiple subscriptions in Laravel Spark with Paddle, you need to handle the paddle_id uniquely for each team. The issue arises because the paddle_id is being reused across different teams owned by the same user, which Paddle does not support.
Here's a solution to handle this scenario:
-
Override the
paddleIdmethod in yourTeammodel: This will ensure that each team has a uniquepaddle_id. -
Generate a unique identifier for each team: You can use a UUID or any other unique identifier for each team.
Here's how you can implement this:
-
Update the
Teammodel:
use Spark\Billable;
use Illuminate\Support\Str;
class Team extends JetstreamTeam
{
use Billable;
// Ensure each team has a unique paddle_id
public function paddleId(): string
{
if (is_null($this->attributes['paddle_id'])) {
$this->attributes['paddle_id'] = (string) Str::uuid();
$this->save();
}
return $this->attributes['paddle_id'];
}
public function paddleEmail(): string|null
{
return $this->owner->email;
}
}
-
Migrate the
teamstable to includepaddle_id:
If your teams table does not already have a paddle_id column, you need to add it. Create a new migration:
php artisan make:migration add_paddle_id_to_teams_table --table=teams
Then, update the migration file:
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class AddPaddleIdToTeamsTable extends Migration
{
public function up()
{
Schema::table('teams', function (Blueprint $table) {
$table->uuid('paddle_id')->nullable()->unique();
});
}
public function down()
{
Schema::table('teams', function (Blueprint $table) {
$table->dropColumn('paddle_id');
});
}
}
Run the migration:
php artisan migrate
-
Ensure
paddle_idis set when creating a new team:
You can set the paddle_id when a new team is created. This can be done in the Team model's creating event:
protected static function booted()
{
static::creating(function ($team) {
if (is_null($team->paddle_id)) {
$team->paddle_id = (string) Str::uuid();
}
});
}
With these changes, each team will have a unique paddle_id, allowing users to own multiple teams and manage multiple subscriptions without conflicts.
I hope this helps! Let me know if you have any further questions.
Best, LaracastsGPT