Skip to content

Commit a965161

Browse files
authored
Merge pull request #28 from chikenare/main
Multiple features can now be used with the same slug
2 parents 6edeef7 + 8fda023 commit a965161

File tree

3 files changed

+27
-0
lines changed

3 files changed

+27
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
use Illuminate\Database\Migrations\Migration;
6+
use Illuminate\Database\Schema\Blueprint;
7+
use Illuminate\Support\Facades\Schema;
8+
9+
return new class extends Migration
10+
{
11+
public function up(): void
12+
{
13+
Schema::table(config('laravel-subscriptions.tables.features'), function (Blueprint $table): void {
14+
$table->dropUnique(config('laravel-subscriptions.tables.features') . '_slug_unique');
15+
$table->unique(['plan_id', 'slug']);
16+
});
17+
}
18+
};

src/Models/Feature.php

+8
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
use Illuminate\Database\Eloquent\Model;
1010
use Illuminate\Database\Eloquent\Relations\HasMany;
1111
use Illuminate\Database\Eloquent\SoftDeletes;
12+
use InvalidArgumentException;
1213
use Laravelcm\Subscriptions\Services\Period;
1314
use Laravelcm\Subscriptions\Traits\BelongsToPlan;
1415
use Laravelcm\Subscriptions\Traits\HasSlug;
@@ -102,13 +103,20 @@ protected static function boot(): void
102103
static::deleted(function (Feature $feature): void {
103104
$feature->usage()->delete();
104105
});
106+
107+
static::creating(function (Feature $feature) {
108+
if (static::where('plan_id', $feature->plan_id)->where('slug', $feature->slug)->exists()) {
109+
throw new InvalidArgumentException('Each plan should only have one feature with the same slug');
110+
}
111+
});
105112
}
106113

107114
public function getSlugOptions(): SlugOptions
108115
{
109116
return SlugOptions::create()
110117
->doNotGenerateSlugsOnUpdate()
111118
->generateSlugsFrom('name')
119+
->allowDuplicateSlugs()
112120
->saveSlugsTo('slug');
113121
}
114122

src/SubscriptionServiceProvider.php

+1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ public function configurePackage(Package $package): void
2020
'create_plan_subscriptions_table',
2121
'create_plan_subscription_usage_table',
2222
'remove_unique_slug_on_subscriptions_table',
23+
'update_unique_keys_on_features_table',
2324
])
2425
->hasInstallCommand(function (InstallCommand $command): void {
2526
$command

0 commit comments

Comments
 (0)