eloquent-versioning icon indicating copy to clipboard operation
eloquent-versioning copied to clipboard

ForceDelete model

Open ghost opened this issue 6 years ago • 2 comments

$storedFile = Storedfile::findOrFail($id);
$storedFile->forceDelete();

Outputs error: Call to a member function toArray() on array exception | Symfony\Component\Debug\Exception\FatalThrowableError file | \vendor\proai\eloquent-versioning\src\BuilderTrait.php

ghost avatar May 30 '18 22:05 ghost

I have the same issue with normal delete:

$storedFile = Storedfile::findOrFail($id); $storedFile->delete();

The fix from netcom does not help.

Are there some news about this?

10bias avatar Jul 13 '18 12:07 10bias

I'm using a custom BuilderTrait (to patch few bugs). Try this for your case @10bias :



    /**
     * Get affected records.
     *
     * @return array
     */
    protected function getAffectedRecords()
    {
        // model only
        if ($this->model->getKey()) {
            $records = [$this->model];
        }

        // mass assignment
        else {
            $records = $this->query->get()->all();
        }

        return $records;
    }

    /**
     * Run the default delete function on the builder.
     *
     * @return mixed
     */
    public function forceDelete()
    {
        // get records
        $affectedRecords = $this->getAffectedRecords();
        $ids = array_map(function($record) {
            return $record->{$this->model->getKeyName()};
        }, $affectedRecords);

        // delete main table records
        if (! $this->query->delete()) {
            return false;
        }

        // delete version table records
        $db = $this->model->getConnection();
        return $db->table($this->model->getVersionTable())
            ->whereIn($this->model->getVersionKeyName(), $ids)
            ->delete();
    }

Mortred974 avatar Feb 22 '19 12:02 Mortred974