michaeldyrynda / laravel-cascade-soft-deletes

Cascading deletes for Eloquent models that implement soft deletes
https://dyrynda.com.au
MIT License
985 stars 75 forks source link

Issue in pivot soft delete cascading #48

Closed AliSuliman01 closed 3 years ago

AliSuliman01 commented 3 years ago

the package works perfect for one-to-many relationship, but when it comes to many-to-many relationship it's not working here is my code :

    class Product extends Model
    {
        use HasFactory, SoftDeletes,CascadeSoftDeletes;
        protected $table = 'products';
        protected $cascadeDeletes  = ['product_categories_2'];

        public function product_categories_2(){
            return $this->belongsToMany(Category::class,'product_filter_categories')->using(ProductFilterCategory::class);
        }
    }

is it a wrong in my code or this package don't support many-to-many relation ?

michaeldyrynda commented 3 years ago

Are you able to clarify how it’s not working?

AliSuliman01 commented 3 years ago

ya sure... here is products migration :

public function up()
    {
        Schema::create('products', function (Blueprint $table) {
            $table->id();

            $table->timestamps();
            $table->softDeletes();

        });
    }

and here is product filter categories migration :

public function up()
    {
        Schema::create('product_filter_categories', function (Blueprint $table) {
            $table->id();
            $table->unsignedBigInteger('category_id');
            $table->unsignedBigInteger('product_id');

            $table->foreign('product_id')
                ->references('id')
                ->on('products')
                ->onDelete('cascade');

            $table->foreign('category_id')
                ->references('id')
                ->on('categories')
                ->onDelete('cascade');

            $table->timestamps();
            $table->softDeletes();
        });
    }

when i soft delete a product's recored as following :

        $product = Product::find($product->id);
        $product->delete();

related recoreds at product_filter_categories table are not soft deleted !

edit 1 : and here is ProductFilterCategory model :

class ProductFilterCategory extends Pivot
{
    use HasFactory, SoftDeletes;

    protected $table = "product_filter_categories";
    protected $guarded = [
        'id',
        'created_at',
        'updated_at',
        'deleted_at',
    ];
}
dvlpr91 commented 3 years ago

Hi...

I need this package, but if this is true, I'm worried.

michaeldyrynda commented 3 years ago

I've done some investigation and it looks like the delete method of the AsPivot trait is called, rather than the one provided by SoftDeletes for the pivot model (via $model->pivot->delete()).

It must have something to do with the way we're calling delete, though, because the soft delete works if you call it on the pivot model directly. Needs some more investigation, but would appreciate any help you can offer on that front.

I've never used soft deletes on the pivot table personally, so no direct experience with it.

dvlpr91 commented 3 years ago

I've done some investigation and it looks like the delete method of the AsPivot trait is called, rather than the one provided by SoftDeletes for the pivot model (via $model->pivot->delete()).

It must have something to do with the way we're calling delete, though, because the soft delete works if you call it on the pivot model directly. Needs some more investigation, but would appreciate any help you can offer on that front.

I've never used soft deletes on the pivot table personally, so no direct experience with it.

Nice to meet you.

The prerequisites for this issue seem odd.

In order to soft delete from the pivot table, the pivot model must be changed to extend the 'Model' class, not the 'Pivot' class.

So, from my point of view, it doesn't appear to be a problem.

dvlpr91 commented 3 years ago

I've done some investigation and it looks like the delete method of the AsPivot trait is called, rather than the one provided by SoftDeletes for the pivot model (via $model->pivot->delete()). It must have something to do with the way we're calling delete, though, because the soft delete works if you call it on the pivot model directly. Needs some more investigation, but would appreciate any help you can offer on that front. I've never used soft deletes on the pivot table personally, so no direct experience with it.

Nice to meet you.

The prerequisites for this issue seem odd.

In order to soft delete from the pivot table, the pivot model must be changed to extend the 'Model' class, not the 'Pivot' class.

So, from my point of view, it doesn't appear to be a problem.

I don't expect this to be a problem with this package.

For artifact creators to use "use SoftDeletes", artifact creators need to change the inheritance of the pivot model.

https://laravel.com/docs/8.x/eloquent-relationships#defining-custom-intermediate-table-models

dvlpr91 commented 3 years ago

I expected a problem with this package. So I implemented the delete function myself. Interestingly, it's identical to your code.

I will use this package. If there are any problems, I will contribute a pull request.

michaeldyrynda commented 3 years ago

Right, there’s even a note that specifically says you can’t use SoftDeletes on a model extending the Pivot relation, you need to extend from Model.

BB93F49A-6005-42F4-9D0C-D745F55E2CFB