Skip to content

Method Illuminate\Database\Eloquent\Collection::delete does not exist #76

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

Open
TantonFirst opened this issue Feb 18, 2025 · 4 comments
Open

Comments

@TantonFirst
Copy link

Method Illuminate\Database\Eloquent\Collection::delete does not exist at /var/www/vendor/dyrynda/laravel-cascade-soft-deletes/src/CascadeSoftDeletes.php(69)

Version: 4.4.1
$fetchMethod = 'chunk';
relation HasMany

@michaeldyrynda
Copy link
Owner

There's not a lot to go by in your issue description, but are you able to provide a minimum reproducible repo to better demonstrate the issue?

@danharper83
Copy link

Hi,

I'm getting the same issue but only when using chunk. I'm performing the initial delete with a filamentphp bulk action.

laravel/framework v11.43.0
filament/filament v3.2.135

Cheers Dan

@michaeldyrynda
Copy link
Owner

Hey @danharper83 - as above, are you able to provide a minimum reproducible repo to work with?

Sounds like in a chunk context, the cascading delete isn't called on a discrete model, but rather the whole collection, so the error makes sense in that regard.

@patrickcarlohickman
Copy link

PR #74 added in the chunk functionality. The issue is that when you use the chunk functionality, the callback being passed into the chunk() method was only designed to expect a model instance, not a Collection of models. So, the callback is accidentally calling delete() on the Collection.

Relevant code:

    protected function cascadeSoftDeletes($relationship)
    {
        $delete = $this->forceDeleting ? 'forceDelete' : 'delete';

        $cb = function($model) use ($delete) {
            isset($model->pivot) ? $model->pivot->{$delete}() : $model->{$delete}();
        };

        $this->handleRecords($relationship, $cb);
    }

    private function handleRecords($relationship, $cb)
    {
        $fetchMethod = $this->fetchMethod ?? 'get';

        if ($fetchMethod == 'chunk') {
            // $cb called here gets the Collection
            $this->{$relationship}()->chunk($this->chunkSize ?? 500, $cb);
        } else {
            foreach($this->{$relationship}()->$fetchMethod() as $model) {
                // $cb called here gets the model instance
                $cb($model);
            }
        }
    }

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

4 participants