PHP code example of laravel-afterburner / voting

1. Go to this page and download the library: Download laravel-afterburner/voting library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

laravel-afterburner / voting example snippets


use Afterburner\Voting\Concerns\HasVoting;

class Team extends JetstreamTeam
{
    use HasVoting;
}

namespace App\Strata\Voting;

use Afterburner\Voting\Contracts\VoterEligibilityResolver;
use Afterburner\Voting\Models\Ballot;
use Afterburner\Voting\Support\VoterUnit;
use App\Models\Property;
use App\Models\User;
use Illuminate\Support\Collection;

class PropertyVoterEligibilityResolver implements VoterEligibilityResolver
{
    public function eligibleVoterUnits(User $user, Ballot $ballot): Collection
    {
        return Property::query()
            ->where('team_id', $ballot->team_id)
            ->where(function ($query) use ($user) {
                $query->where('designated_voter_id', $user->id)
                    ->orWhereHas('activeProxies', fn ($q) => $q->where('proxy_holder_user_id', $user->id));
            })
            ->get()
            ->map(fn (Property $property) => new VoterUnit(Property::class, $property->id))
            ->reject(fn (VoterUnit $unit) => $this->alreadyVoted($ballot, $unit));
    }

    public function totalEligibleVoterUnits(Ballot $ballot): int
    {
        return Property::query()->where('team_id', $ballot->team_id)->count();
    }

    public function canCastVote(User $user, Ballot $ballot, string $voterUnitType, int $voterUnitId): bool
    {
        return $this->eligibleVoterUnits($user, $ballot)->contains(
            fn (VoterUnit $unit) => $unit->matches($voterUnitType, $voterUnitId)
        );
    }

    public function voterUnitLabel(string $voterUnitType, int $voterUnitId): string
    {
        $property = Property::query()->find($voterUnitId);

        return $property ? 'Lot '.$property->lot_number : 'Property #'.$voterUnitId;
    }

    protected function alreadyVoted(Ballot $ballot, VoterUnit $unit): bool
    {
        return $ballot->responses()
            ->where('voter_unit_type', $unit->type)
            ->where('voter_unit_id', $unit->id)
            ->exists();
    }
}

use Afterburner\Voting\Contracts\ProvidesWeightedVotes;
use Afterburner\Voting\Contracts\VoterEligibilityResolver;

class PropertyVoterEligibilityResolver implements ProvidesWeightedVotes, VoterEligibilityResolver
{
    public function voterUnitWeight(Ballot $ballot, string $voterUnitType, int $voterUnitId): float
    {
        $property = Property::query()->find($voterUnitId);

        return (float) ($property?->vote_weight ?? 1);
    }
}

AFTERBURNER_VOTING_CUSTOM_ELECTORATE_RESOLVER=App\Voting\MyCustomElectorateResolver
bash
php artisan migrate
bash
php artisan vendor:publish --tag=afterburner-voting-assets --force
php artisan view:clear