Installation Add the bundle via Composer:
composer require xlabs/competitionbundle
Register the bundle in config/app.php under providers:
Xlabs\CompetitionBundle\CompetitionBundle::class,
Publish Config & Migrations Run:
php artisan vendor:publish --provider="Xlabs\CompetitionBundle\CompetitionBundle" --tag="config"
php artisan vendor:publish --provider="Xlabs\CompetitionBundle\CompetitionBundle" --tag="migrations"
Migrate the database:
php artisan migrate
First Use Case: Creating a Competition
Define a competition in config/competition.php:
'competitions' => [
'summer_challenge' => [
'name' => 'Summer Coding Challenge',
'start_date' => '2024-06-01',
'end_date' => '2024-08-31',
'rules' => 'Submit 3 PRs weekly.',
],
],
Trigger a competition via a controller:
use Xlabs\CompetitionBundle\Services\CompetitionService;
public function startCompetition(CompetitionService $competitionService) {
$competitionService->activate('summer_challenge');
}
Competition Lifecycle Management
CompetitionService to toggle competitions dynamically:
$competitionService->activate('summer_challenge');
$competitionService->deactivate('summer_challenge');
CompetitionListener to auto-activate/deactivate based on start_date/end_date in config.Participant Tracking
// In a middleware
$user->competitions()->attach('summer_challenge');
$user->competitions()->wherePivot('active', true)->get();
Scoring & Leaderboards
Competition model to add custom scoring logic:
// app/Models/Competition.php
public function calculateScore(User $user) {
return $user->submissions()->where('competition_id', $this->id)->count();
}
$leaderboard = Competition::with('participants')
->where('active', true)
->get()
->sortByDesc(function ($competition) {
return $competition->participants->count();
});
Event-Driven Extensions
CompetitionActivated) to trigger side effects:
// EventSubscriber
public function onCompetitionActivated(CompetitionActivated $event) {
// Send notifications, log analytics, etc.
}
dispatch(new CalculateCompetitionScores($competitionId));
Route::apiResource('competitions', CompetitionController::class)->middleware('auth:sanctum');
Config Overrides
config/competition.php is not cached during development:
php artisan config:clear
'start_date' => env('COMPETITION_SUMMER_START', '2024-06-01'),
Migration Conflicts
competitions table, create a new migration after publishing the bundle’s migrations to avoid overwrites.Circular Dependencies
CompetitionService in model boot methods—use observers or events instead to prevent lazy-loading issues.Time Zone Handling
$competition->start_date->setTimezone($user->timezone);
public function onCompetitionActivated(CompetitionActivated $event) {
\Log::debug('Activated:', [$event->competition->name]);
}
app/Http/Kernel.php.Custom Competition Types
Competition model and create a trait for shared logic:
// app/Models/CustomCompetition.php
class CustomCompetition extends Competition {
use HasCustomRules;
}
Validation Rules
CompetitionRequest (if the bundle provides one) or create a form request:
public function rules() {
return [
'name' => 'required|unique:competitions,name,'.$this->competition->id,
'start_date' => 'required|date|after:yesterday',
];
}
Third-Party Integrations
// Listen for CompetitionActivated
event(new \App\Events\SyncCompetitionToDiscord($event->competition));
How can I help you explore Laravel packages today?