feat: ✨ Remove relation between stats and urls

This commit is contained in:
Noah Lancien 2026-06-18 10:59:44 +02:00
parent 5262941eab
commit 479cb39cc0
7 changed files with 100 additions and 29 deletions

View file

@ -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');
}

View file

@ -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.']);

View file

@ -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,
],
);

View file

@ -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);
}
}

View file

@ -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);
}
}

View file

@ -0,0 +1,38 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
/**
* Run the migrations.
*/
public function up(): void
{
// Urls.
Schema::table('urls', function (Blueprint $table) {
$table->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');
});
}
};

View file

@ -0,0 +1,53 @@
<?php
use App\Models\UrlStats;
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
/**
* Run the migrations.
*/
public function up(): void
{
Schema::table('url_stats', function (Blueprint $table) {
$table->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']);
});
}
};