Clone model with nested relationships
I would like to make a week planner for an organisation. I would be recommended that the administrator can make a template weekplanner, duplicate it for a specific week and make some changes to it.
The weekplanner looks something like this:
On a database level
- Schedule Table
Schedule_id Schedule_name
- Workshop Table
Workshop_id Workshop_name (like bakery, garden) Schedule_id (relationship to the Schedule table)
The Schedule has many workshops, every workshop belongsTo a schedule
- WorkshopSession table
WorkshopSession_id Date/day of week Teacher (related with teachers table)
- A many to many relationship beween the WorkshopSession and the students table. The workshopSession hasMany students, the student hasMany workshops.
I would like to be able to clone a schedule (with all relationships, all the way down to the many to many relationship.
I'm quite new to this.
I know that cloning a model is easy, like this
$clone = $model->replicate();
$clone->push();
But how do I also make a database copy of the workshops related to the schedule, the sessions related to the workshops and the students in the many to many relationship with the session?
Thanks! 🙏
Please or to participate in this conversation.