PHP code example of masterix21 / laravel-bookings

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

    

masterix21 / laravel-bookings example snippets


use Masterix21\Bookings\Models\Concerns\Bookable;
use Masterix21\Bookings\Models\Concerns\IsBookable;

class Room extends Model implements Bookable
{
    use IsBookable;

    protected $fillable = ['name', 'capacity'];
}

$room = Room::create(['name' => 'Deluxe Suite', 'capacity' => 4]);

$room->bookableResource()->create([
    'max' => 1,            // Maximum concurrent bookings (null = unlimited)
    'size' => 4,           // Resource capacity
    'is_bookable' => true,
    'is_visible' => true,
]);

use Masterix21\Bookings\Actions\BookResource;
use Spatie\Period\Period;
use Spatie\Period\PeriodCollection;

$periods = PeriodCollection::make([
    Period::make('2026-12-25', '2026-12-27'),
]);

$booking = (new BookResource())->run(
    periods: $periods,
    bookableResource: $room->bookableResource,
    booker: auth()->user(),
    label: 'Christmas Holiday',
    meta: ['guests' => 2],
);

use Masterix21\Bookings\Exceptions\BookingResourceOverlappingException;

try {
    $booking = (new BookResource())->run(/* ... */);
} catch (BookingResourceOverlappingException $e) {
    return response()->json(['error' => 'Time slot already booked'], 409);
}

$room->isBookedAt(now());            // bool
$room->bookedPeriodsOfDate(today()); // booked periods for a date
$room->bookings;                     // all bookings for the resource
bash
php artisan vendor:publish --tag="bookings-migrations"
php artisan migrate
bash
php artisan vendor:publish --tag="bookings-config"