Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Clone BelongsToMany relation: Add new rows #47

Open
ahoiroman opened this issue Dec 28, 2020 · 1 comment
Open

Clone BelongsToMany relation: Add new rows #47

ahoiroman opened this issue Dec 28, 2020 · 1 comment

Comments

@ahoiroman
Copy link

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?

@weotch
Copy link
Member

weotch commented Dec 30, 2020

You could use the onCloning() handler or the events Cloner fires to add logic that would copy the relationships on your many to many.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants