Askedio / laravel-soft-cascade

Cascade Delete & Restore when using Laravel SoftDeletes
https://medium.com/asked-io/cascading-softdeletes-with-laravel-5-a1a9335a5b4d
MIT License
705 stars 63 forks source link

How to skip a relationship when I do a cascade deletion? #80

Closed aronzillo closed 6 years ago

aronzillo commented 6 years ago

Hi!, I have a "Issuer" model with a one-to-many relationship with "Branch", "Receivers", "Product", if I delete a "Issuer" the related models are correctly deleted in cascade, but, if I delete a "Branch" (which has a one-to-many relationship with "Phone", "Series") "Branch", "Phone", "Series" are deleted because they are dependent on "Branch" and also deletes the "Issuer" model, although "Issuer" and "Branch" have a relationship, how can I avoid that by eliminating a "Branch" the "Issuer" or some other relationship is not deleted?

maguilar92 commented 6 years ago

I do not understand what you want to say, can you give an example of how you have defined the models?

Also with this what do you mean? If I delete a "Branch" the related models are correctly deleted in cascade, but, if I delete a "Branch"

aronzillo commented 6 years ago

Sorry was a drafting error. What I wanted to say is: If I delete a "Issuer" the related models are correctly deleted in cascade, but, if I delete a "Branch"...

maguilar92 commented 6 years ago

@aronzillo Can you give an example of how you have defined the models?

aronzillo commented 6 years ago

@maguilar92 These are some models that I have, the migrations have column $table-> softDeletes(), in models I omitted some parts, by the way, the Many To Many relation in my case the branch_user table the deleted_at column always stays in null. Am I doing something wrong?

<?php

namespace App\Models;

use Askedio\SoftCascade\Traits\SoftCascadeTrait;
use Illuminate\Database\Eloquent\{ Model, SoftDeletes };

class Issuer extends Model
{
    use SoftDeletes, SoftCascadeTrait;

    protected $dates = ['deleted_at'];

    protected $softCascade = ['branches', 'receivers', 'products'];

    public function branches()
    {
        return $this->hasMany(Branch::class);
    }

    public function receivers()
    {
        return $this->hasMany(Receiver::class);
    }

    public function products()
    {
        return $this->hasMany(Product::class);
    }
}
<?php

namespace App\Models;

use Askedio\SoftCascade\Traits\SoftCascadeTrait;
use Illuminate\Database\Eloquent\{ Model, SoftDeletes };

class Branch extends Model
{
    use SoftDeletes, SoftCascadeTrait;

    protected $dates = ['deleted_at'];

    protected $softCascade = ['series', 'subusers'];

    public function issuer()
    {
        return $this->belongsTo(Issuer::class);
    }

    public function subusers()
    {
        return $this->belongsToMany(User::class)->withTimestamps();
    }

    public function series()
    {
        return $this->hasMany(Serie::class);
    }
}
<?php

namespace App\Models;

use Askedio\SoftCascade\Traits\SoftCascadeTrait;
use Illuminate\Database\Eloquent\{ Model, SoftDeletes };

class Receiver extends Model
{
    use SoftDeletes, SoftCascadeTrait;

    protected $dates = ['deleted_at'];

    public function issuer()
    {
        return $this->belongsTo(Issuer::class);
    }
}
<?php

namespace App\Models;

use Askedio\SoftCascade\Traits\SoftCascadeTrait;
use Illuminate\Database\Eloquent\{ Model, SoftDeletes };

class Product extends Model
{
    use SoftDeletes, SoftCascadeTrait;

    protected $dates = ['deleted_at'];

    public function issuer()
    {
        return $this->belongsTo(Issuer::class);
    }
}
<?php

namespace App\Models;

use Illuminate\Database\Eloquent\SoftDeletes;
use Askedio\SoftCascade\Traits\SoftCascadeTrait;
use Illuminate\Foundation\Auth\User as Authenticatable;

class User extends Authenticatable
{
    use Notifiable, SoftDeletes, SoftCascadeTrait;

    protected $dates = ['deleted_at'];

    protected $softCascade = ['subusers'];

    public function issuers()
    {
        return $this->hasMany(Issuer::class);
    }

    public function profile()
    {
        return $this->hasOne(UserProfile::class);
    }

    public function subusers()
    {
        return $this->belongsToMany(Branch::class)->withTimestamps();
    }
}
maguilar92 commented 6 years ago

I checked the code you paste me and as I can see when you delete a Branch the Issuer will not be deleted because the relationship with Issuer is not defined in any model in $softCascade.

aronzillo commented 6 years ago

I don't know what happened, but when I delete the vendor folder and run composer install everything worked fine, thanks for your help and sorry for the inconvenience.

maguilar92 commented 6 years ago

@aronzillo Thanks to you. I guess it's because you did not have the latest version installed.