diff --git a/app/Http/Controllers/Bo/StatsController.php b/app/Http/Controllers/Bo/StatsController.php index 856fdea..44f21d2 100644 --- a/app/Http/Controllers/Bo/StatsController.php +++ b/app/Http/Controllers/Bo/StatsController.php @@ -166,9 +166,9 @@ class StatsController extends Controller */ private function getCallsSum(Model $entity, Carbon $date, PeriodEnum $period): int { - $urlIds = method_exists($entity, 'urls') && $entity->urls()->exists() - ? $entity->urls()->pluck('id') - : collect([$entity->getKey()]); + $urlKeys = method_exists($entity, 'urls') && $entity->urls()->exists() + ? $entity->urls()->pluck('key') + : collect([$entity->key]); return UrlStats::query() ->when( @@ -189,7 +189,7 @@ class StatsController extends Controller $date->copy()->endOfMonth() ]) ) - ->whereIn('url_id', $urlIds) + ->whereIn('url_key', $urlKeys) ->sum('calls'); } diff --git a/app/Http/Controllers/Bo/UrlController.php b/app/Http/Controllers/Bo/UrlController.php index 1b997d8..c742a93 100644 --- a/app/Http/Controllers/Bo/UrlController.php +++ b/app/Http/Controllers/Bo/UrlController.php @@ -197,9 +197,7 @@ class UrlController extends Controller foreach ($url->history as $history) { $history->deleteOrFail(); } - foreach ($url->stats as $stats) { - $stats->deleteOrFail(); - } + $url->deleteOrFail(); return redirect()->route('bo.urls.index') ->with(['warning' => 'L\'url pour la clé ' . $url->key . ' a bien été supprimée.']); diff --git a/app/Jobs/IncrementUrlStat.php b/app/Jobs/IncrementUrlStat.php index fb5ca78..f14df05 100644 --- a/app/Jobs/IncrementUrlStat.php +++ b/app/Jobs/IncrementUrlStat.php @@ -45,7 +45,8 @@ class IncrementUrlStat implements ShouldQueue ->whereDate('date', now()) ->firstOrNew( [ - 'url_id' => $this->url->id, + 'url_key' => $this->url->key, + 'url' => $this->url->value, ], ); diff --git a/app/Models/Url.php b/app/Models/Url.php index e815411..8a563e5 100644 --- a/app/Models/Url.php +++ b/app/Models/Url.php @@ -76,14 +76,4 @@ class Url extends Model { return $this->hasMany(UrlHistory::class); } - - /** - * Get the stats for the url. - * - * @return \Illuminate\Database\Eloquent\Relations\HasMany<\App\Models\UrlStats, $this> - */ - public function stats() - { - return $this->hasMany(UrlStats::class); - } } diff --git a/app/Models/UrlStats.php b/app/Models/UrlStats.php index f3bcb43..6ad5cc2 100644 --- a/app/Models/UrlStats.php +++ b/app/Models/UrlStats.php @@ -17,7 +17,8 @@ class UrlStats extends Model protected $fillable = [ 'calls', 'date', - 'url_id', + 'url_key', + 'url', ]; /** @@ -41,14 +42,4 @@ class UrlStats extends Model $model->date = now(); }); } - - /** - * Get the url that owns the stats. - * - * @return \Illuminate\Database\Eloquent\Relations\BelongsTo<\App\Models\Url, $this> - */ - public function url() - { - return $this->belongsTo(Url::class); - } } diff --git a/database/migrations/2026_06_12_110749_add_cascade_on_delete.php b/database/migrations/2026_06_12_110749_add_cascade_on_delete.php new file mode 100644 index 0000000..7358667 --- /dev/null +++ b/database/migrations/2026_06_12_110749_add_cascade_on_delete.php @@ -0,0 +1,38 @@ +dropForeign(['brand_id']); + + $table->foreign('brand_id') + ->references('id') + ->on('brands') + ->cascadeOnDelete(); + }); + } + + /** + * Reverse the migrations. + */ + public function down(): void + { + Schema::table('urls', function (Blueprint $table) { + $table->dropForeign(['brand_id']); + $table->foreign('brand_id') + ->references('id') + ->on('brands') + ->onDelete('no action'); + }); + } +}; diff --git a/database/migrations/2026_06_12_115131_alter_url_stats_table.php b/database/migrations/2026_06_12_115131_alter_url_stats_table.php new file mode 100644 index 0000000..6eb2da1 --- /dev/null +++ b/database/migrations/2026_06_12_115131_alter_url_stats_table.php @@ -0,0 +1,53 @@ +string('url_key', 45)->after('id')->comment('Url key. No foreign key to allow url deletion and keep stats.'); + $table->string('url')->after('url_key')->comment('Current url.'); + }); + + DB::statement( + "UPDATE url_stats + INNER JOIN urls ON url_stats.url_id = urls.id + SET url_stats.url_key = urls.key, + url_stats.url = urls.value" + ); + + Schema::table('url_stats', function (Blueprint $table) { + $table->dropForeign(['url_id']); + $table->dropColumn(['url_id']); + }); + } + + /** + * Reverse the migrations. + */ + public function down(): void + { + Schema::table('url_stats', function (Blueprint $table) { + $table->foreignId('url_id')->default(1)->after('date')->constrained('urls'); + }); + + DB::statement( + "UPDATE url_stats + INNER JOIN urls ON url_stats.url_key = urls.key + SET url_stats.url_id = urls.id" + ); + + Schema::table('url_stats', function (Blueprint $table) { + $table->dropColumn(['url_key', 'url']); + }); + } +};