PHP code example of da-sie / livewire-calendar

1. Go to this page and download the library: Download da-sie/livewire-calendar 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/ */

    

da-sie / livewire-calendar example snippets


use Asantibanez\LivewireCalendar\LivewireCalendar;
use Illuminate\Support\Collection;
use Carbon\Carbon;

class AppointmentsCalendar extends LivewireCalendar
{
    public function events(): Collection
    {
        return collect([
            [
                'id' => 1,
                'title' => 'Breakfast',
                'description' => 'Pancakes!',
                'date' => Carbon::today(),
            ],
            [
                'id' => 2,
                'title' => 'Meeting',
                'description' => 'Work stuff',
                'date' => Carbon::tomorrow(),
            ],
        ]);
    }
}

[
    'id' => 1,
    'title' => 'Conference',
    'description' => 'Annual tech conference',
    'date' => '2026-04-08',
    'date_end' => '2026-04-10',
]

public function events(): Collection
{
    return Event::query()
        ->whereDate('date', '<=', $this->gridEndsAt)
        ->where(function ($q) {
            $q->whereDate('date_end', '>=', $this->gridStartsAt)
              ->orWhereNull('date_end');
        })
        ->get()
        ->map(fn (Event $e) => [
            'id' => $e->id,
            'title' => $e->title,
            'description' => $e->notes,
            'date' => $e->date,
            'date_end' => $e->date_end,
        ]);
}

$this->setViewMode('week');  // 'month', 'week', 'day'

public function onDayClick($year, $month, $day)
{
    // Triggered when a day cell is clicked
}

public function onEventClick($eventId)
{
    // Triggered when an event is clicked
}

public function onEventDropped($eventId, $year, $month, $day)
{
    // Triggered when an event is dragged and dropped to another day
    // $year/$month/$day = target day
}

use Livewire\Attributes\On;

#[On('calendar-event-dropped')]
public function handleEventDrop($eventId, $targetYear, $targetMonth, $targetDay, $sourceYear, $sourceMonth, $sourceDay)
{
    $event = Event::find($eventId);

    // Calculate how many days the event was moved
    $source = Carbon::create($sourceYear, $sourceMonth, $sourceDay);
    $target = Carbon::create($targetYear, $targetMonth, $targetDay);
    $diff = $source->diffInDays($target, false);

    // Shift both start and end dates by the same offset
    $event->update([
        'date' => Carbon::parse($event->date)->addDays($diff),
        'date_end' => $event->date_end ? Carbon::parse($event->date_end)->addDays($diff) : null,
    ]);
}
bash
php artisan make:livewire AppointmentsCalendar
bash
php artisan vendor:publish --tag=livewire-calendar
bash
php artisan vendor:publish --tag=livewire-calendar-assets