|
9 | 9 | use Bkwld\Decoy\Models\Image; |
10 | 10 | use Decoy; |
11 | 11 | use Illuminate\Database\Eloquent\Builder; |
| 12 | +use Illuminate\Database\Eloquent\SoftDeletingScope; |
12 | 13 |
|
13 | 14 | /** |
14 | 15 | * Enable logging changes to models |
@@ -48,7 +49,8 @@ public function changes() |
48 | 49 | */ |
49 | 50 | public static function bootLoggable() |
50 | 51 | { |
51 | | - // Automatically eager load the images relationship |
| 52 | + // Add scope that will fetch trashed versions of models when the |
| 53 | + // Change::QUERY_KEY is present. |
52 | 54 | static::addGlobalScope(static::$LOGGABLE_SCOPE, function (Builder $builder) { |
53 | 55 | static::showTrashedVersion($builder); |
54 | 56 | }); |
@@ -83,7 +85,7 @@ private static function showTrashedVersion(Builder $builder) |
83 | 85 | { |
84 | 86 | if (($change = static::lookupRequestedChange()) |
85 | 87 | && static::builderMatchesChange($change, $builder)) { |
86 | | - static::includeTrashed($change, $builder); |
| 88 | + $builder->withoutGlobalScope(SoftDeletingScope::class); |
87 | 89 | } |
88 | 90 | } |
89 | 91 |
|
@@ -149,28 +151,6 @@ private static function builderMatchesChange(Change $change, Builder $builder) |
149 | 151 | }); |
150 | 152 | } |
151 | 153 |
|
152 | | - /** |
153 | | - * Manually remove already added soft deleted where conditions. This is |
154 | | - * necessary since withTrashed() can't be called on a Builder. |
155 | | - * http://yo.bkwld.com/1K04151B2h3M |
156 | | - * |
157 | | - * @param Change $change |
158 | | - * @param Builder $builder |
159 | | - * @return void |
160 | | - */ |
161 | | - private static function includeTrashed(Change $change, Builder $builder) |
162 | | - { |
163 | | - $class = $change->model; |
164 | | - $table = (new $class)->getTable(); |
165 | | - foreach($builder->getQuery()->wheres as $key => $where) { |
166 | | - if ($where['column'] == $table.'.deleted_at' |
167 | | - && $where['type'] == 'Null') { |
168 | | - unset($builder->getQuery()->wheres[$key]); |
169 | | - break; |
170 | | - } |
171 | | - } |
172 | | - } |
173 | | - |
174 | 154 | /** |
175 | 155 | * Replace all the attributes with those from the specified Change specified |
176 | 156 | * in the reqeust query. |
|
0 commit comments