eloquent-versioning
eloquent-versioning copied to clipboard
ForceDelete model
$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
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?
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();
}