jqh 5 年之前
父节点
当前提交
b3fe0446f2
共有 1 个文件被更改,包括 17 次插入29 次删除
  1. 17 29
      src/Repositories/EloquentRepository.php

+ 17 - 29
src/Repositories/EloquentRepository.php

@@ -38,6 +38,11 @@ class EloquentRepository extends Repository implements TreeRepository
      */
     protected $relations = [];
 
+    /**
+     * @var \Illuminate\Database\Eloquent\Collection
+     */
+    protected $collection;
+
     /**
      * EloquentRepository constructor.
      *
@@ -442,24 +447,23 @@ class EloquentRepository extends Repository implements TreeRepository
     /**
      * 删除数据.
      *
-     * @param Form $form
+     * @param Form  $form
+     * @param array $originalData
      *
      * @return bool
      */
-    public function destroy(Form $form, array $deletingData)
+    public function destroy(Form $form, array $originalData)
     {
-        $id = $form->getKey();
+        $models = $this->collection->keyBy($this->getKeyName());
 
-        $deletingData = collect($deletingData)->keyBy($this->getKeyName());
+        collect(explode(',', $form->getKey()))->filter()->each(function ($id) use ($form, $models) {
+            $model = $models->get($id);
 
-        collect(explode(',', $id))->filter()->each(function ($id) use ($form, $deletingData) {
-            $data = $deletingData->get($id, []);
-
-            if (! $data) {
+            if (! $model) {
                 return;
             }
 
-            $model = $this->createDeletingModel($id, $data);
+            $data = $model->toArray();
 
             if ($this->isSoftDeletes && $model->trashed()) {
                 $form->deleteFiles($data, true);
@@ -476,23 +480,6 @@ class EloquentRepository extends Repository implements TreeRepository
         return true;
     }
 
-    /**
-     * @param mixed $id
-     * @param array $data
-     *
-     * @return \Illuminate\Database\Eloquent\Model
-     */
-    protected function createDeletingModel($id, $data)
-    {
-        $model = $this->createEloquent($data);
-
-        $model->{$model->getKeyName()} = $id;
-
-        $model->exists = true;
-
-        return $model;
-    }
-
     /**
      * 查询删除前的行数据.
      *
@@ -510,13 +497,14 @@ class EloquentRepository extends Repository implements TreeRepository
 
         $id = $form->getKey();
 
-        return $query
+        $this->collection = $query
             ->with($this->getRelations())
             ->findOrFail(
                 collect(explode(',', $id))->filter()->toArray(),
                 $this->getFormColumns()
-            )
-            ->toArray();
+            );
+
+        return $this->collection->toArray();
     }
 
     /**