PHP code example of cheesytech / booking

1. Go to this page and download the library: Download cheesytech/booking 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/ */

    

cheesytech / booking example snippets


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,
        ],
    ],
];

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; }
}

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)
);

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();
});

// 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();

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

$booking = Booking::factory()->pending()->create();
$room = Room::factory()->create();
$user = User::factory()->create();
bash
php artisan vendor:publish --provider="CheeasyTech\Booking\BookingServiceProvider" --tag="migrations"
php artisan migrate
bash
php artisan vendor:publish --provider="CheeasyTech\Booking\BookingServiceProvider" --tag="config"
bash
php artisan package:install cheesytech/booking