From 532f294a77fb578bf68cb1c1c6601f21f37e0f76 Mon Sep 17 00:00:00 2001 From: Noah Lancien Date: Fri, 5 Jan 2024 12:29:13 +0100 Subject: [PATCH] Reverse query to get participations with the current code --- app/Models/Code.php | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/app/Models/Code.php b/app/Models/Code.php index 70c4447..9ff810e 100644 --- a/app/Models/Code.php +++ b/app/Models/Code.php @@ -161,10 +161,11 @@ class Code extends Model $settings = app(Settings::class); return Attribute::make( get: fn (): bool => $settings->nb_unique_code_use ? - Participation::where( - 'code_unique_id', - $this->id - )->count() >= $settings->nb_unique_code_use : false + Code::query()->where('id', $this->id)->whereHas( + 'participations', + operator: '>=', + count: $settings->nb_unique_code_use + )->count() : false ); } }