diff --git a/app/Concerns/HasSlug.php b/app/Concerns/HasSlug.php index 19315cd..4ae4aa5 100644 --- a/app/Concerns/HasSlug.php +++ b/app/Concerns/HasSlug.php @@ -39,7 +39,7 @@ protected function fillSlugs(): void $this->slug = Str::slug($this->slug); - if (! $this->slug || $this->slugAlreadyUsed($this->slug)) { + if (blank($this->slug) || $this->slugAlreadyUsed($this->slug)) { $this->slug = $this->generateSlug(); } } diff --git a/app/Console/Commands/Import/ImportElectionsCommand.php b/app/Console/Commands/Import/ImportElectionsCommand.php index 95e6ddd..dac5957 100644 --- a/app/Console/Commands/Import/ImportElectionsCommand.php +++ b/app/Console/Commands/Import/ImportElectionsCommand.php @@ -6,6 +6,7 @@ use App\Enums\ElectionType; use App\Models\Election; +use Carbon\Carbon; use stdClass; class ImportElectionsCommand extends Command @@ -45,16 +46,27 @@ public function handle(): int ); $query->each(function (stdClass $row) { + $type = match ($row->BallotType) { + 0 => ElectionType::REFERENDUM, + 1 => ElectionType::PRESIDENTIAL, + 2,3 => ElectionType::PARLIAMENTARY, + 7 => ElectionType::EURO, + default => ElectionType::LOCAL, + }; + + $date = Carbon::parse($row->Date); + + $slug = match ($type) { + ElectionType::PRESIDENTIAL => "prezidentiale-{$row->Name}-{$date->year}", + ElectionType::EURO => "europarlamentare {$date->year}", + default => "{$row->Name}-{$date->year}", + }; + Election::create([ 'title' => $row->Name, - 'type' => match ($row->BallotType) { - 0 => ElectionType::REFERENDUM, - 1 => ElectionType::PRESIDENTIAL, - 2,3 => ElectionType::PARLIAMENTARY, - 7 => ElectionType::EURO, - default => ElectionType::LOCAL, - }, - 'date' => $row->Date, + 'slug' => $slug, + 'type' => $type, + 'date' => $date->toDateString(), 'is_live' => false, 'has_lists' => match ($row->BallotType) { // Referendum = 0, diff --git a/app/Livewire/Pages/ElectionResults.php b/app/Livewire/Pages/ElectionResults.php index b99d5c4..58974bc 100644 --- a/app/Livewire/Pages/ElectionResults.php +++ b/app/Livewire/Pages/ElectionResults.php @@ -8,6 +8,7 @@ use App\Models\Party; use App\Models\Record; use App\Models\Vote; +use Illuminate\Database\Eloquent\Builder; use Illuminate\Support\Collection; use Illuminate\Support\Facades\DB; use Illuminate\Support\Number; @@ -34,6 +35,9 @@ public function parties(): Collection { return Party::query() ->whereBelongsTo($this->election) + ->whereHas('votes', function (Builder $query) { + $query->whereBelongsTo($this->election); + }) ->with('media') ->get(); } @@ -43,6 +47,9 @@ public function candidates(): Collection { return Candidate::query() ->whereBelongsTo($this->election) + ->whereHas('votes', function (Builder $query) { + $query->whereBelongsTo($this->election); + }) ->with('media') ->get(); } diff --git a/app/Models/Election.php b/app/Models/Election.php index ac41230..56f16c3 100644 --- a/app/Models/Election.php +++ b/app/Models/Election.php @@ -13,7 +13,6 @@ use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Relations\HasMany; -use Illuminate\Support\Str; class Election extends Model implements HasName, HasAvatar { @@ -56,10 +55,6 @@ protected static function booted(): void ->orderByDesc('year') ->orderByDesc('is_live'); }); - - static::creating(function (self $model) { - $model->slug = Str::slug("{$model->title}-{$model->date->year}"); - }); } public function scheduledJobs(): HasMany