Skip to content

Commit e5e4e16

Browse files
committed
Add resetScope() after applyScope() for some methods
1 parent a2ede73 commit e5e4e16

File tree

1 file changed

+34
-0
lines changed

1 file changed

+34
-0
lines changed

src/Eloquent/BaseRepository.php

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,7 @@ public function first($columns = ['*']): mixed
149149
$result = $this->model->first($columns);
150150

151151
$this->resetModel();
152+
$this->resetScope();
152153

153154
return $result;
154155
}
@@ -163,6 +164,7 @@ public function firstWhere(array $where, $columns = ['*']): mixed
163164
$result = $this->model->first($columns);
164165

165166
$this->resetModel();
167+
$this->resetScope();
166168

167169
return $result;
168170
}
@@ -175,6 +177,7 @@ public function firstOrNew(array $attributes, array $values = []): mixed
175177
$model = $this->model->firstOrNew($attributes, $values);
176178

177179
$this->resetModel();
180+
$this->resetScope();
178181

179182
return $model;
180183
}
@@ -186,12 +189,15 @@ public function firstOrCreate(array $attributes, array $values = [], bool $witho
186189

187190
if (!is_null($model = $this->model->where($attributes)->first())) {
188191
$this->resetModel();
192+
$this->resetScope();
189193
return $model;
190194
}
191195

192196
$method = $withoutEvents ? 'saveQuietly' : 'save';
193197
$model = tap($this->model->newModelInstance([...$attributes, ...$values]), fn ($instance) => $instance->{$method}());
198+
194199
$this->resetModel();
200+
$this->resetScope();
195201

196202
event(new RepositoryEntityCreated($this, $model));
197203

@@ -216,6 +222,7 @@ public function paginate($limit = null, $columns = ['*'], $method = "paginate"):
216222
$results->appends(app('request')->query());
217223

218224
$this->resetModel();
225+
$this->resetScope();
219226

220227
return $results;
221228
}
@@ -229,8 +236,11 @@ public function find(mixed $id, $columns = ['*']): mixed
229236
{
230237
$this->applyCriteria();
231238
$this->applyScope();
239+
232240
$model = $this->model->findOrFail($id, $columns);
241+
233242
$this->resetModel();
243+
$this->resetScope();
234244

235245
return $model;
236246
}
@@ -239,8 +249,11 @@ public function findByField(string $field, $value = null, $columns = ['*']): mix
239249
{
240250
$this->applyCriteria();
241251
$this->applyScope();
252+
242253
$model = $this->model->where($field, '=', $value)->get($columns);
254+
243255
$this->resetModel();
256+
$this->resetScope();
244257

245258
return $model;
246259
}
@@ -253,7 +266,9 @@ public function findWhere(array $where, $columns = ['*']): mixed
253266
$this->applyConditions($where);
254267

255268
$model = $this->model->get($columns);
269+
256270
$this->resetModel();
271+
$this->resetScope();
257272

258273
return $model;
259274
}
@@ -262,8 +277,11 @@ public function findWhereIn($field, array $values, $columns = ['*']): mixed
262277
{
263278
$this->applyCriteria();
264279
$this->applyScope();
280+
265281
$model = $this->model->whereIn($field, $values)->get($columns);
282+
266283
$this->resetModel();
284+
$this->resetScope();
267285

268286
return $model;
269287
}
@@ -272,8 +290,11 @@ public function findWhereNotIn($field, array $values, $columns = ['*']): mixed
272290
{
273291
$this->applyCriteria();
274292
$this->applyScope();
293+
275294
$model = $this->model->whereNotIn($field, $values)->get($columns);
295+
276296
$this->resetModel();
297+
$this->resetScope();
277298

278299
return $model;
279300
}
@@ -282,8 +303,11 @@ public function findWhereBetween($field, array $values, $columns = ['*']): mixed
282303
{
283304
$this->applyCriteria();
284305
$this->applyScope();
306+
285307
$model = $this->model->whereBetween($field, $values)->get($columns);
308+
286309
$this->resetModel();
310+
$this->resetScope();
287311

288312
return $model;
289313
}
@@ -325,6 +349,7 @@ public function update(array $attributes, int $id, bool $withoutEvents = false):
325349
$model->{$method}();
326350

327351
$this->resetModel();
352+
$this->resetScope();
328353

329354
event(new RepositoryEntityUpdated($this, $model));
330355

@@ -342,6 +367,7 @@ public function updateWhere(array $where, array $attributes, bool $withoutEvents
342367
event(new RepositoryEntityUpdated($this, $this->model->getModel()));
343368

344369
$this->resetModel();
370+
$this->resetScope();
345371

346372
return $updated;
347373
}
@@ -352,7 +378,9 @@ public function updateOrCreate(array $attributes, array $values = [], bool $with
352378

353379
$method = $withoutEvents ? 'saveQuietly' : 'save';
354380
$model = tap($this->model->firstOrNew($attributes), fn ($instance) => $instance->fill($values)->{$method}());
381+
355382
$this->resetModel();
383+
$this->resetScope();
356384

357385
event(new RepositoryEntityUpdated($this, $model));
358386

@@ -366,6 +394,7 @@ public function upsert(array $values, array|string $uniqueBy, ?array $update = n
366394
event(new RepositoryEntityCreated($this, $this->model->getModel()));
367395

368396
$this->resetModel();
397+
$this->resetScope();
369398

370399
return $upserted;
371400
}
@@ -386,6 +415,7 @@ public function deleteWhere(array $where, bool $forceDelete = false): ?bool
386415
event(new RepositoryEntityDeleted($this, $this->model->getModel()));
387416

388417
$this->resetModel();
418+
$this->resetScope();
389419

390420
return $deleted;
391421
}
@@ -399,6 +429,7 @@ public function massDelete(): ?bool
399429
event(new RepositoryEntityDeleted($this, $this->model->getModel()));
400430

401431
$this->resetModel();
432+
$this->resetScope();
402433

403434
return $deleted;
404435
}
@@ -556,6 +587,8 @@ public function getBaseQuery(): QueryBuilder
556587
$query = $this->model->toBase();
557588

558589
$this->resetModel();
590+
$this->resetScope();
591+
559592
return $query;
560593
}
561594

@@ -591,6 +624,7 @@ protected function manageDeletes(int $id, string $method)
591624
$originalModel = clone $model;
592625

593626
$this->resetModel();
627+
$this->resetScope();
594628

595629
$model->{$method}();
596630

0 commit comments

Comments
 (0)