spatie / opening-hours

Query and format a set of opening hours

Home Page:https://freek.dev/595-managing-opening-hours-with-php

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Use this library with eloquent model

hmunyeku opened this issue · comments

Dear
Is it possible to use your library with user eloquent model to know their availability?
How can I do that?
THanks

commented

Hello, opening-hours relies on its data array. So you can easily store the whole data as a string in a column of your model using json_encode/serialize then restore it with json_decode/unserialize.

For a more structured approach (store atomic data), if we omit the meta-data, you can simplify the structure for each business entity to:

[
    $day => [$timeRange...],
    'exceptions' => [
        $date => [$timeRange...]
    ],
]

Each business entry -> N day definitions + N exceptions
1 day -> 1 name + N time ranges
1 exception -> 1 date (with or without year) + N time ranges
1 time range -> start hour (int) + start minute (int) + end hour (int) + end minute (int)
So 4 tables :)

1 time range -> start hour (int) + start minute (int) + end hour (int) + end minute (int)

@kylekatarnls why store as int instead of datetime ?

commented

Datetime doest not fit as hour can be from 0 to 24 included, datetime has hour from 0 to 23 only. Then you would store useless date + second. Datetime is designed for a precise moment not recurrent time-only data, that's definitly not an appropriated type.

Hello, here you go:
Extend OpeningHours:
image

Create json field in table, cast field as json and in your model create custom setters/getters:
image