1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Orkhanahmadov\EloquentRepository\Repository\Concerns; |
4
|
|
|
|
5
|
|
|
use Illuminate\Database\Eloquent\Model; |
6
|
|
|
use Illuminate\Database\Eloquent\Builder; |
7
|
|
|
use Illuminate\Database\Eloquent\Collection; |
8
|
|
|
use Orkhanahmadov\EloquentRepository\Repository\Contracts\Cacheable; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* @property-read Builder|Model $model |
12
|
|
|
* @method Builder|Model find($modelId) |
13
|
|
|
* @method void invalidateCache($model) |
14
|
|
|
* @mixin \Orkhanahmadov\EloquentRepository\EloquentRepository |
15
|
|
|
*/ |
16
|
|
|
trait DeletesEntity |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* Finds a model with ID and deletes it. |
20
|
|
|
* |
21
|
|
|
* @param int|string $modelId |
22
|
|
|
* |
23
|
|
|
* @return bool|mixed|null |
24
|
|
|
* @throws \Exception |
25
|
|
|
*/ |
26
|
|
|
public function findAndDelete($modelId) |
27
|
|
|
{ |
28
|
|
|
$model = $this->find($modelId); |
29
|
|
|
|
30
|
|
|
return $this->delete($model); |
|
|
|
|
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Deletes a model. |
35
|
|
|
* |
36
|
|
|
* @param Model $model |
37
|
|
|
* |
38
|
|
|
* @return bool|mixed|null |
39
|
|
|
* @throws \Exception |
40
|
|
|
*/ |
41
|
|
|
public function delete($model) |
42
|
|
|
{ |
43
|
|
|
if ($this instanceof Cacheable) { |
44
|
|
|
$this->invalidateCache($model); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
return $model->delete(); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Finds a soft deleted model with given ID and restores it. |
52
|
|
|
* |
53
|
|
|
* @param int|string $modelId |
54
|
|
|
* |
55
|
|
|
* @return bool|null |
56
|
|
|
*/ |
57
|
|
|
public function findAndRestore($modelId) |
58
|
|
|
{ |
59
|
|
|
$model = $this->findFromTrashed($modelId); |
60
|
|
|
|
61
|
|
|
return $this->restore($model); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Finds a soft deleted model with given ID. |
66
|
|
|
* |
67
|
|
|
* @param int|string $modelId |
68
|
|
|
* |
69
|
|
|
* @return Builder|Builder[]|Collection|Model|null |
70
|
|
|
*/ |
71
|
|
|
public function findFromTrashed($modelId) |
72
|
|
|
{ |
73
|
|
|
if (! method_exists($this->entity, 'restore')) { |
74
|
|
|
throw new \BadMethodCallException('Model is not using "soft delete" feature.'); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
$model = $this->model->onlyTrashed()->find($modelId); |
78
|
|
|
|
79
|
|
|
if (! $model) { |
80
|
|
|
$this->throwModelNotFoundException($modelId); |
|
|
|
|
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
return $model; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Restores soft deleted model. |
88
|
|
|
* |
89
|
|
|
* @param Model $model |
90
|
|
|
* |
91
|
|
|
* @return bool|null |
92
|
|
|
*/ |
93
|
|
|
public function restore($model) |
94
|
|
|
{ |
95
|
|
|
if (! method_exists($this->entity, 'restore')) { |
96
|
|
|
throw new \BadMethodCallException('Model is not using "soft delete" feature.'); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
return $model->restore(); |
100
|
|
|
} |
101
|
|
|
} |
102
|
|
|
|