Installation Add the package via Composer:
composer require chill-project/event
Publish the migration and config files:
php artisan vendor:publish --provider="ChillProject\Event\EventServiceProvider" --tag="migrations"
php artisan vendor:publish --provider="ChillProject\Event\EventServiceProvider" --tag="config"
Run the migrations:
php artisan migrate
First Use Case: Creating an Event Define an event in your database or via a seeder:
use ChillProject\Event\Models\Event;
$event = Event::create([
'title' => 'Summer Hackathon',
'description' => 'Join us for a weekend of coding!',
'start_at' => now()->addDays(7),
'end_at' => now()->addDays(8),
'max_participants' => 50,
]);
Registering a User to an Event
Use the register() method on the event model:
$user = auth()->user();
$event->register($user);
Checking Participation Verify if a user is registered:
if ($event->isRegistered($user)) {
// User is registered
}
Event Management
create, update, find, etc.) on the Event model.status field (e.g., draft, published, cancelled) to control visibility and participation.
$event->publish(); // Manually set status to 'published'
Participation Handling
$event->registerMany([$user1, $user2, $user3]);
$event->unregister($user);
Event Validation
max_participants):
use Illuminate\Support\Facades\Validator;
$validator = Validator::make($eventData, [
'max_participants' => 'integer|min:1',
'start_at' => 'required|date|after:today',
]);
Event Listeners and Observers
// app/Observers/EventObserver.php
class EventObserver {
public function registered($event) {
Notification::send($event->user, new EventRegistered($event));
}
}
EventServiceProvider:
Event::observe(EventObserver::class);
API Integration
// routes/api.php
Route::post('/events/{event}/register', [EventController::class, 'register']);
public function register(Event $event) {
$event->register(auth()->user());
return response()->json(['message' => 'Registered successfully']);
}
Custom Event Types
Event model to support custom fields (e.g., virtual, location):
class CustomEvent extends Event {
protected $casts = [
'is_virtual' => 'boolean',
'location' => 'string',
];
}
Participation Limits
max_participants is validated before registration to avoid database constraint errors. Override the register() method if custom logic is needed:
public function register(User $user) {
if ($this->participants()->count() >= $this->max_participants) {
throw new \Exception("Event is full.");
}
$this->participants()->attach($user);
}
Time Zone Handling
start_at and end_at in UTC and convert to user time zones when displaying:
$event->start_at->setTimezone($user->timezone);
Observer Conflicts
App\Observers\EventObserver).Migration Conflicts
events table, run php artisan migrate:fresh cautiously to avoid data loss. Backup first.Soft Deletes
Event model uses soft deletes by default. Use withTrashed() or forceDelete() explicitly if needed:
Event::withTrashed()->where('title', 'Hackathon')->get();
Participation Queries
dd($event->participants()->count());
Event Status Logic
public function scopePublished($query) {
return $query->where('status', 'published');
}
Usage:
Event::published()->get();
Logging Registrations
\Log::info("User {$user->id} registered for event {$event->id}");
Custom Participation Rules
canRegister() method to add logic (e.g., age restrictions):
public function canRegister(User $user) {
return $user->age >= 18 && parent::canRegister($user);
}
Event Categories/Tags
Category model:
public function categories() {
return $this->belongsToMany(Category::class);
}
Webhook Notifications
laravel-webhooks):
Event::updated(function ($event) {
\Webhook::send('event_updated', $event);
});
Recurring Events
spatie/laravel-calendar):
public function occurrences() {
return (new RecurrenceRule($this->recurrence_rule))
->occurrences($this->start_at, $this->end_at);
}
How can I help you explore Laravel packages today?