Skip to content

Commit

Permalink
Merge pull request #28 from chikenare/main
Browse files Browse the repository at this point in the history
Multiple features can now be used with the same slug
  • Loading branch information
mckenziearts authored Dec 3, 2024
2 parents 6edeef7 + 8fda023 commit a965161
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 0 deletions.
18 changes: 18 additions & 0 deletions database/migrations/update_unique_keys_on_features_table.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

declare(strict_types=1);

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
public function up(): void
{
Schema::table(config('laravel-subscriptions.tables.features'), function (Blueprint $table): void {
$table->dropUnique(config('laravel-subscriptions.tables.features') . '_slug_unique');
$table->unique(['plan_id', 'slug']);
});
}
};
8 changes: 8 additions & 0 deletions src/Models/Feature.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\SoftDeletes;
use InvalidArgumentException;
use Laravelcm\Subscriptions\Services\Period;
use Laravelcm\Subscriptions\Traits\BelongsToPlan;
use Laravelcm\Subscriptions\Traits\HasSlug;
Expand Down Expand Up @@ -102,13 +103,20 @@ protected static function boot(): void
static::deleted(function (Feature $feature): void {
$feature->usage()->delete();
});

static::creating(function (Feature $feature) {
if (static::where('plan_id', $feature->plan_id)->where('slug', $feature->slug)->exists()) {
throw new InvalidArgumentException('Each plan should only have one feature with the same slug');
}
});
}

public function getSlugOptions(): SlugOptions
{
return SlugOptions::create()
->doNotGenerateSlugsOnUpdate()
->generateSlugsFrom('name')
->allowDuplicateSlugs()
->saveSlugsTo('slug');
}

Expand Down
1 change: 1 addition & 0 deletions src/SubscriptionServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ public function configurePackage(Package $package): void
'create_plan_subscriptions_table',
'create_plan_subscription_usage_table',
'remove_unique_slug_on_subscriptions_table',
'update_unique_keys_on_features_table',
])
->hasInstallCommand(function (InstallCommand $command): void {
$command
Expand Down

0 comments on commit a965161

Please sign in to comment.