twoarmtom's avatar

Inserting the information for a range of dates

I'm trying to find examples of properly looping through a range of dates to insert information.

Currently, I am creating a schedule row with the user, location etc., and a single date in a form using:

Schedule::create($request->all());

But I would like to add start and end date and create identical row information (user, location, etc.), looping through each date in the range, and stopping after the end date is inserted. Assuming two dates would be something like:

$request->start_date
$request->end_date

I found some examples with messy PHP, but I'm assuming there is a more 'eloquent' way to do this. Any help would be greatly appreciated.

Thank you.

0 likes
2 replies
Ashraam's avatar
Ashraam
Best Answer
Level 41

You could try the CarbonPeriod like this

$period = CarbonPeriod::create($request()->only('start_date', 'end_date'));

foreach($period as $date) {
    // Do your stuff
}

Please or to participate in this conversation.