Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Bervetuna's avatar

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:

https://imgur.com/a/FRkBMJw

On a database level

  1. Schedule Table

Schedule_id Schedule_name

  1. 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

  1. WorkshopSession table

WorkshopSession_id Date/day of week Teacher (related with teachers table)

  1. 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! 🙏

0 likes
4 replies
Bervetuna's avatar

@Sinnbeck Thanks.

Does that also takes care of the nested relationships (as described in my post)?

Sinnbeck's avatar

@Bervetuna Sadly the docs does not state whether it does or not, but I would think it would be easy to test?

If not, you can look at the source to find out how it works, and implement your own better logic

Please or to participate in this conversation.