I have a section in my Laravel Nova from where I can create a schedule for classes. In the front-end, my teachers can easily create many classes from a single form by defining a repeat_till and repeat_frequency fields. How can I go about achieving the same results in Laravel Nova?
My own controller has the following code:
if($data['repeat'] && $data['repeat_till']) {
$next_date = Carbon::parse($data['date']);
$end_date = Carbon::parse($data['repeat_till']);
while($next_date <= $end_date) {
$dates[] = $next_date->format('Y-m-d');
$next_date = $next_date->addDays($data['repeat']);
}
} else {
$dates[] = $data['date'];
}
...
$parent = null;
foreach($dates as $date) {
$schedule = Schedule::create([
...
'date' => $date,
'parent_id' => ($parent ? $parent->id : null),
...
]);
if(!$parent) {
$parent = $schedule;
}
}
I've looked at the possibility of using Observers for this and so far that seems like the only viable option. However, for that I will need to have access to the repeat(repeat_frequency) and repeat_till values, which are not part of the Schedule model. How can I go about accessing these values in the created method?
You can do so by overriding the save function inside your model public function save(array $options = array())
and since you don't have access to the repeat_frequency and repeat_till values inside your observer, i'm guessing these aren't database fields.. so what i recommend is unsetting these variables before saving the model.. EX: