I’m trying to retrieve data from a model, then rewriting it to the appropriate format for the Spatie Icalendar, but then it doesn’t work, because of multiple events and not only a single one.
I have tried many different solutions, but didn’t find one that’s working.
Please, how would you iterate over the many events in the Spatie?
Spatie Documentation: https://github.com/spatie/icalendar-generator
I would like to iterate through all the calendar events, creating an array for the spatie.
It should end up in an ICAL format, that’s extractable for normal calendars.
2
Answers
A lot of inconsistencies in the code, that i think makes it way harder than it have to be. Classes named events as properties, inconsistent naming conventions, model in plural (i know, class is a reserved keyword) etc.
Simply loop over the events and add the model data one by one to the
spatie
package.I would assume it requires an end time, so i added one hour.
start_time
on the model should be added to the$dates
array to be casted to aCarbon
object. Instead of doing for loops, i use the collection methods to create similar logic, read about them here. Which is the type that the query builder returns.The documentation states you can add an array of events to a calendar:
Here are some examples in pseudo-code, adapt to your real objects:
Add multiple events:
Or better yet, add an array of events: