RafaelMunoznl
2 months ago
347
11
Laravel

Trying to addHours using Carbon I get: Unexpected data found.

Posted 2 months ago by RafaelMunoznl

I have the following Model "Openinghour" wich has following attributes:

    protected $fillable = [
        'company_id', 'open_at', 'close_at', 'working_day', 'pause'
    ];

I am using Carbon (imported at the top): use Carbon\Carbon;

and declaring: protected $dates = ['open_at', 'close_at', 'pause'];

Now I want to get a collection with hours per day like this:

[
    monday => { "09:00:00", "10:00:00", "11:00:00", "12:00:00", "13:00:00", "14:00:00", "15:00:00", "16:00:00", "17:00:00"}
    tuesday => {....}
    wednesday => {....}
    thursday => {....}
    friday => {....}
    saturday => {"09:00:00", "10:00:00", "11:00:00", "12:00:00", "13:00:00"}
]

For that I did:

    public static function getOpenings($id)
    {
        $openingHours = Openinghour::where('company_id', '=', $id)->get();

        $data = [];
        $weekday = [];
        foreach ($openingHours as $day) {
            array_push($weekday, $day->working_day);
            for ($t = $day->open_at; $t < $day->close_at; $t->addHour()) {
                $weekday[count($weekday - 1)] = $t;
            };
        }
        
        return $openingHours;
    }
}

But I get the following error: Unexpected data found. Unexpected data found. Data missing

What am I missing?

Please sign in or create an account to participate in this conversation.