Clone an Eloquent object including all relationships?

Stefan Izdrail

Founder & Senior Architect · 2026-06-29

Laravel Company
Title: Effortlessly Clone an Eloquent Object Including All Relationships Using Laravel

Cloning an Eloquent object with all its relationships is an essential skill for any developer working with Laravel. This process allows for the creation of a new instance that mirrors the original one while at the same time, maintaining the relationships established between other models.

To begin, we will create three tables in our database: Users, Roles, and User_Roles. These tables represent the relationships among users, their roles, and the association between them through the user_roles table.

users ( id, name, email )
roles ( id, name )
user_roles ( user_id, role_id )

Next, we create Eloquent models for each table:

class User extends Model {
    public function roles() {
        return $this->hasMany('App\Role', 'user_roles');
    }
}

class Role extends Model {}

With our database and models in place, we can now proceed to clone an Eloquent object and attach all its relationships.

$user = User::find(1);
$new_user = $user->clone();
echo $new_user->name . ' (' . $new_user->email . ')'; // Existing user's name and email in new instance

In the above example, we retrieve a user from the database using the find() method. To clone it, we call the clone() method on this object. This will create a new instance of User with all its data but a distinct ID.

However, as mentioned earlier, cloning an Eloquent object does not include its relationships by default. To achieve that, we need to utilize the hasMany method defined in our User model. The hasMany() method returns the relationship between users and their roles:

$user = User::find(1);
$new_user = $user->clone();
$roles = $user->roles()->get(); // Retrieve user's roles
$new_user->roles()->sync($roles); // Attach these roles to the new instance
echo $new_user->name . ' (' . $new_user->email . ')'; // Existing user's name and email in new instance with assigned roles

In this example, we retrieve all the user's roles (i.e., rows from the role table connected to the original user) using the hasMany relationship. We then attach these roles to the newly cloned user by syncing them together.

To conclude, you can easily clone an Eloquent object and its relationships by following these steps:

1. Create models for your database tables and define their relationships. 2. Retrieve the original model instance using find() or any other suitable method. 3. Clone the instance using the clone() method. 4. If necessary, retrieve the related data (e.g., roles) using the hasMany relationship. 5. Sync the related data to the new cloned instance. 6. Use the resulting object as you need in your application. By following these steps, you can efficiently create new instances of Eloquent objects along with their relationships and ensure a smooth development process.