cheesytech/booking 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

cheesytech/booking

Composer 安装命令:

composer require cheesytech/booking

包简介

A flexible and powerful booking system for Laravel applications

README 文档

README

Packagist Version PHP Version Laravel License Tests codecov

Laravel Booking

A flexible and powerful booking system for Laravel applications that supports polymorphic relationships, time slot management, and advanced status tracking.

Features

  • 🕒 Time slot management with overlap prevention
  • 🔄 Polymorphic relationships for bookable and bookerable resources
  • ⚙️ Configurable validation and overlap rules
  • 🎯 Custom booking rules via OverlapRule interface
  • 📅 Business hours and custom rules validation
  • 🔒 Booking duration and minimum interval limits
  • 🎨 Easy to extend with traits and interfaces
  • 📊 Status tracking with full history and metadata
  • 🔄 Event-driven architecture (BookingCreated, BookingUpdated, etc.)
  • 🧪 Comprehensive testing support and model factories
  • 🔍 Advanced querying and duration-based scopes (cross-DB)
  • 💾 Multi-database support (MySQL, PostgreSQL, SQLite, SQL Server)
  • ⏱️ Flexible duration calculations (minutes, hours, days)
  • 🛠️ Publishable config and migration files

Requirements

  • PHP 8.1 or higher
  • Laravel 10.0, 11.0, 12.0, or 13.0
  • Carbon 2.0 or higher

Installation

  1. Install the package via composer:
composer require cheesytech/booking
  1. The package will automatically register its service provider.

  2. Publish and run the migrations:

php artisan vendor:publish --provider="CheeasyTech\Booking\BookingServiceProvider" --tag="migrations"
php artisan migrate
  1. Publish the configuration file:
php artisan vendor:publish --provider="CheeasyTech\Booking\BookingServiceProvider" --tag="config"

Or use the install command for all at once:

php artisan package:install cheesytech/booking

This will create a config/booking.php file in your config directory.

Configuration

The package is highly configurable through the config/booking.php file. Example:

return [
    'statuses' => [
        'pending' => [
            'label' => 'Pending',
            'color' => '#FFA500',
            'can_transition_to' => ['confirmed', 'cancelled'],
        ],
        'confirmed' => [
            'label' => 'Confirmed',
            'color' => '#008000',
            'can_transition_to' => ['cancelled', 'completed'],
        ],
        'cancelled' => [
            'label' => 'Cancelled',
            'color' => '#FF0000',
            'can_transition_to' => [],
        ],
        'completed' => [
            'label' => 'Completed',
            'color' => '#0000FF',
            'can_transition_to' => [],
        ],
    ],
    'overlap' => [
        'enabled' => true,
        'allow_same_booker' => false,
        'min_time_between' => 0,
        'max_duration' => 0,
        'rules' => [
            'business_hours' => [
                'enabled' => false,
                'class' => \CheeasyTech\Booking\Rules\BusinessHoursRule::class,
            ],
        ],
    ],
    'events' => [
        'enabled' => true,
        'classes' => [
            'created' => \CheeasyTech\Booking\Events\BookingCreated::class,
            'updated' => \CheeasyTech\Booking\Events\BookingUpdated::class,
            'deleted' => \CheeasyTech\Booking\Events\BookingDeleted::class,
            'status_changed' => \CheeasyTech\Booking\Events\BookingStatusChanged::class,
        ],
    ],
];

Model Setup

Implement the provided interfaces and use the traits for your models:

use CheeasyTech\Booking\Contracts\Bookable;
use CheeasyTech\Booking\Traits\HasBookings;
use Illuminate\Database\Eloquent\Model;

class Room extends Model implements Bookable {
    use HasBookings;
    // ...
    public function getBookableId(): int { return $this->id; }
    public function getBookableType(): string { return static::class; }
}

use CheeasyTech\Booking\Contracts\Bookerable;
use CheeasyTech\Booking\Traits\HasBookers;

class User extends Model implements Bookerable {
    use HasBookers;
    // ...
    public function getBookerableId(): int|string { return $this->id; }
    public function getBookerableType(): string { return static::class; }
}

Quick Start

use CheeasyTech\Booking\Models\Booking;
use Carbon\Carbon;

$room = Room::find(1);
$user = User::find(1);

$booking = new Booking();
$booking->bookable()->associate($room);
$booking->bookerable()->associate($user);
$booking->start_time = Carbon::tomorrow()->setHour(10);
$booking->end_time = Carbon::tomorrow()->setHour(11);
$booking->status = 'pending';
$booking->save();

// Change booking status
$booking->changeStatus('confirmed', 'Approved by admin', ['key' => 'value']);

// Check for overlap
$isAvailable = !$booking->hasOverlap(
    Carbon::tomorrow()->setHour(10),
    Carbon::tomorrow()->setHour(11)
);

Traits & Interfaces

  • HasBookings: Add to bookable models (e.g., Room) for convenient booking management (newBooking, deleteBooking, etc.).
  • HasBookers: Add to bookerable models (e.g., User) for managing bookings made by the entity.
  • Bookable: Interface for resources to be booked (must implement getBookableId, getBookableType).
  • Bookerable: Interface for entities making bookings (must implement getBookerableId, getBookerableType).
  • OverlapRule: Interface for custom overlap validation rules.

Database Schema

The package creates the following database table:

Schema::create('bookings', function (Blueprint $table) {
    $table->id();
    $table->morphs('bookable');
    $table->morphs('bookerable');
    $table->dateTime('start_time');
    $table->dateTime('end_time');
    $table->string('status')->default('pending');
    $table->json('status_history')->nullable();
    $table->timestamp('status_changed_at')->nullable();
    $table->timestamps();
});

Advanced Usage

Query Scopes

// Get bookings longer than 2 hours
Booking::durationLongerThan(120)->get();
// Get bookings between 1 and 2 hours
Booking::durationBetween(60, 120)->get();
// Combine with other conditions
Booking::durationLongerThan(120)->where('status', 'confirmed')->get();

Status Management

$booking->changeStatus('confirmed', 'Approved by supervisor', ['approver_id' => 123]);
$status = $booking->getCurrentStatus();
$history = $booking->getStatusHistory();
if ($booking->hasStatus('confirmed')) { /* ... */ }

Overlap & Custom Rules

  • Prevents overlapping bookings by default.
  • Supports minimum interval and max duration.
  • Add custom rules by implementing OverlapRule and registering in config.
  • Example: BusinessHoursRule restricts bookings to business hours.

Events

Events are fired for all major actions:

  • BookingCreated
  • BookingUpdated
  • BookingDeleted
  • BookingStatusChanged

Testing & Factories

Use provided factories for testing:

$booking = Booking::factory()->pending()->create();
$room = Room::factory()->create();
$user = User::factory()->create();

Updating PHPDoc

To update PHPDoc for models, run:

./update-phpdoc.sh

Contributing

Thank you for considering contributing to the Laravel Booking package! Please feel free to submit pull requests or create issues for bugs and feature requests.

License

The Laravel Booking package is open-sourced software licensed under the MIT license.

cheesytech/booking 适用场景与选型建议

cheesytech/booking 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 1 次下载、GitHub Stars 达 3, 最近一次更新时间为 2025 年 06 月 17 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「calendar」 「laravel」 「booking」 「scheduler」 「reservation」 「appointments」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 cheesytech/booking 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 cheesytech/booking 我们能提供哪些服务?
定制开发 / 二次开发

基于 cheesytech/booking 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 1
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 3
  • 点击次数: 25
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 3
  • Watchers: 0
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2025-06-17