Skip to content

Clone BelongsToMany relation: Add new rows #47

@ahoiroman

Description

@ahoiroman

Hello,

I got this model:

<?php

namespace App;

[...]

class Item extends Model
{
    use Cloneable;

    protected $cloneable_relations = ['category', 'children'];

    /**
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
     */
    public function category()
    {
        return $this->belongsTo(Category::class);
    }

    /**
     * @return \Illuminate\Database\Eloquent\Relations\belongsToMany
     */
    public function children(): belongsToMany
    {
        return $this->belongsToMany(Child::class)
            ->withPivot('value')->withTimestamps();
    }

}

Now if I do Item::first()->duplicate(), the category is cloned as new row - but the children are not. I understand the docs that tell me that this is not possible on many to many: But it would be nice if it would be optional. Any ideas on that?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions