masterix21 / laravel-bookings
Add bookings ability to any Eloquent model
Fund package maintenance!
lucalongo
Requires
- php: ^8.3|^8.4
- illuminate/contracts: ^12.0
- kirschbaum-development/eloquent-power-joins: ^4.2
- nesbot/carbon: ^3.8
- spatie/laravel-package-tools: ^1.92
- spatie/period: ^2.4
- staudenmeir/belongs-to-through: ^2.16
- staudenmeir/eloquent-has-many-deep: ^1.20
Requires (Dev)
- mockery/mockery: ^1.5
- orchestra/testbench: ^7.7|^8.0|^9.0|^10.0
- pestphp/pest: ^1.23|^2.1|^3.1
- phpunit/phpunit: ^9.5.24|^10.5|^11.5
- spatie/pest-plugin-test-time: ^1.1|^2.2
This package is auto-updated.
Last update: 2025-07-29 14:13:09 UTC
README
A comprehensive Laravel package that adds powerful booking functionality to any Eloquent model. Transform your models into bookable resources with advanced features like time-based reservations, capacity management, planning constraints, overlap detection, and event-driven architecture.
Features
- ๐ Make any Eloquent model bookable with simple traits
- ๐ Advanced time period management using Spatie Period library
- ๐ข Resource capacity control with configurable limits
- ๐ Planning constraints with weekday and time restrictions
- ๐ Overlap detection and conflict prevention
- ๐ฏ Event-driven architecture for audit trails and integrations
- ๐๏ธ Polymorphic relationships for flexible booker and resource types
- ๐งช Well tested with comprehensive test suite
- โก Performance optimized with efficient database queries
- ๐ก๏ธ Transaction safety with automatic rollback on failures
Installation
Install the package via Composer:
composer require masterix21/laravel-bookings
Publish and run the migrations:
php artisan vendor:publish --tag="bookings-migrations"
php artisan migrate
Optionally, publish the config file:
php artisan vendor:publish --tag="bookings-config"
Quick Start
1. Make a Model Bookable
Add the IsBookable
trait to any model you want to make bookable:
use Masterix21\Bookings\Models\Concerns\IsBookable; use Masterix21\Bookings\Models\Concerns\Bookable; class Room extends Model implements Bookable { use IsBookable; protected $fillable = ['name', 'capacity']; }
2. Create a Bookable Resource
use Masterix21\Bookings\Models\BookableResource; // Create a bookable resource for your room $room = Room::create(['name' => 'Deluxe Suite', 'capacity' => 4]); $bookableResource = BookableResource::create([ 'resource_type' => Room::class, 'resource_id' => $room->id, 'max' => 1, // Maximum concurrent bookings 'size' => 4, // Resource capacity 'is_bookable' => true, 'is_visible' => true, ]);
3. Make a Booking
use Masterix21\Bookings\Actions\BookResource; use Spatie\Period\Period; use Spatie\Period\PeriodCollection; // Create booking periods $periods = PeriodCollection::make([ Period::make('2024-12-25', '2024-12-27'), // 2 nights ]); // Book the resource $booking = (new BookResource())->run( periods: $periods, bookableResource: $bookableResource, booker: auth()->user(), // The user making the booking label: 'Christmas Holiday', note: 'Special dietary requirements', meta: ['guests' => 2, 'payment_method' => 'credit_card'] );
Core Concepts
BookableResource
The central entity that represents a bookable item. It's linked to your actual model (Room, Car, etc.) via polymorphic relationships.
Booking
Represents a reservation with metadata, booker information, and associated time periods.
BookedPeriod
Individual time slots within a booking, supporting complex multi-period reservations.
BookablePlanning
Defines availability rules, working hours, and constraints for resources.
Advanced Features
Planning Constraints
Define when resources are available:
use Masterix21\Bookings\Models\BookablePlanning; BookablePlanning::create([ 'bookable_resource_id' => $bookableResource->id, 'monday' => true, 'tuesday' => true, 'wednesday' => true, 'thursday' => true, 'friday' => true, 'saturday' => false, // Closed on weekends 'sunday' => false, 'starts_at' => '2024-01-01 09:00:00', // Available from 9 AM 'ends_at' => '2024-12-31 18:00:00', // Until 6 PM ]);
Event System
Listen to booking lifecycle events:
use Masterix21\Bookings\Events\BookingCompleted; use Masterix21\Bookings\Events\BookingFailed; // In your EventServiceProvider protected $listen = [ BookingCompleted::class => [ SendBookingConfirmationEmail::class, UpdateInventory::class, ], BookingFailed::class => [ LogBookingFailure::class, NotifyAdministrators::class, ], ];
Checking Availability
// Check if a resource is booked at a specific time $isBooked = $room->isBookedAt(now()); // Get booked periods for a specific date $bookedPeriods = $room->bookedPeriodsOfDate(today()); // Get all bookings for a resource $bookings = $room->bookings;
Overlap Detection
The package automatically prevents overlapping bookings:
use Masterix21\Bookings\Exceptions\BookingResourceOverlappingException; try { $booking = (new BookResource())->run(/* ... */); } catch (BookingResourceOverlappingException $e) { // Handle booking conflict return response()->json(['error' => 'Time slot already booked'], 409); }
Complete Examples
For comprehensive implementation examples, see:
- ๐จ Hotel Booking System - Complete hotel reservation system
- ๐ Car Rental System - Vehicle rental management
- ๐ฝ๏ธ Restaurant Reservations - Table booking system
- ๐ Service Appointments - Appointment scheduling
Documentation
For detailed documentation, see:
- โ๏ธ Configuration - Package configuration options
- ๐๏ธ Database Schema - Database structure and migrations
- ๐ API Reference - Complete API documentation
Key Topics
- ๐๏ธ Architecture - Package design and structure
- ๐ Getting Started - Quick start guide
- ๐ Models - Model relationships and usage
- โก Actions - Core booking operations
- ๐ฏ Events - Event system and listeners
- ๐งช Testing - Testing strategies and examples
- ๐ง Extending - Customization and extensions
- ๐จ Troubleshooting - Common issues and solutions
Legacy Quick Reference
For complete API documentation, see docs/api-reference.md
The package provides the following core traits and actions:
IsBookable
trait - Makes any model bookableHasBookings
trait - For entities that can make bookingsBookResource
action - Creates and updates bookingsCheckBookingOverlaps
action - Validates booking conflicts
Events are automatically fired during the booking lifecycle for audit trails and integrations.
Testing
Run the package tests:
composer test
Run tests with coverage:
composer test-coverage
Run static analysis:
composer analyse
Changelog
Please see CHANGELOG for more information on what has changed recently.
Contributing
Please see CONTRIBUTING for details.
Security Vulnerabilities
Please review our security policy on how to report security vulnerabilities.
Credits
License
The MIT License (MIT). Please see License File for more information.