Subversion-Projekte lars-tiefland.laravel_shop

Revision

Revision 148 | Details | Vergleich mit vorheriger | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
148 lars 1
<?php
2
 
3
namespace Illuminate\Database\Eloquent;
4
 
5
/**
6
 * @method static \Illuminate\Database\Eloquent\Builder|\Illuminate\Database\Query\Builder withTrashed(bool $withTrashed = true)
7
 * @method static \Illuminate\Database\Eloquent\Builder|\Illuminate\Database\Query\Builder onlyTrashed()
8
 * @method static \Illuminate\Database\Eloquent\Builder|\Illuminate\Database\Query\Builder withoutTrashed()
9
 */
10
trait SoftDeletes
11
{
12
    /**
13
     * Indicates if the model is currently force deleting.
14
     *
15
     * @var bool
16
     */
17
    protected $forceDeleting = false;
18
 
19
    /**
20
     * Boot the soft deleting trait for a model.
21
     *
22
     * @return void
23
     */
24
    public static function bootSoftDeletes()
25
    {
26
        static::addGlobalScope(new SoftDeletingScope);
27
    }
28
 
29
    /**
30
     * Initialize the soft deleting trait for an instance.
31
     *
32
     * @return void
33
     */
34
    public function initializeSoftDeletes()
35
    {
36
        if (! isset($this->casts[$this->getDeletedAtColumn()])) {
37
            $this->casts[$this->getDeletedAtColumn()] = 'datetime';
38
        }
39
    }
40
 
41
    /**
42
     * Force a hard delete on a soft deleted model.
43
     *
44
     * @return bool|null
45
     */
46
    public function forceDelete()
47
    {
399 lars 48
        if ($this->fireModelEvent('forceDeleting') === false) {
49
            return false;
50
        }
51
 
148 lars 52
        $this->forceDeleting = true;
53
 
54
        return tap($this->delete(), function ($deleted) {
55
            $this->forceDeleting = false;
56
 
57
            if ($deleted) {
58
                $this->fireModelEvent('forceDeleted', false);
59
            }
60
        });
61
    }
62
 
63
    /**
399 lars 64
     * Force a hard delete on a soft deleted model without raising any events.
65
     *
66
     * @return bool|null
67
     */
68
    public function forceDeleteQuietly()
69
    {
70
        return static::withoutEvents(fn () => $this->forceDelete());
71
    }
72
 
73
    /**
148 lars 74
     * Perform the actual delete query on this model instance.
75
     *
76
     * @return mixed
77
     */
78
    protected function performDeleteOnModel()
79
    {
80
        if ($this->forceDeleting) {
81
            return tap($this->setKeysForSaveQuery($this->newModelQuery())->forceDelete(), function () {
82
                $this->exists = false;
83
            });
84
        }
85
 
86
        return $this->runSoftDelete();
87
    }
88
 
89
    /**
90
     * Perform the actual delete query on this model instance.
91
     *
92
     * @return void
93
     */
94
    protected function runSoftDelete()
95
    {
96
        $query = $this->setKeysForSaveQuery($this->newModelQuery());
97
 
98
        $time = $this->freshTimestamp();
99
 
100
        $columns = [$this->getDeletedAtColumn() => $this->fromDateTime($time)];
101
 
102
        $this->{$this->getDeletedAtColumn()} = $time;
103
 
104
        if ($this->usesTimestamps() && ! is_null($this->getUpdatedAtColumn())) {
105
            $this->{$this->getUpdatedAtColumn()} = $time;
106
 
107
            $columns[$this->getUpdatedAtColumn()] = $this->fromDateTime($time);
108
        }
109
 
110
        $query->update($columns);
111
 
112
        $this->syncOriginalAttributes(array_keys($columns));
113
 
114
        $this->fireModelEvent('trashed', false);
115
    }
116
 
117
    /**
118
     * Restore a soft-deleted model instance.
119
     *
120
     * @return bool
121
     */
122
    public function restore()
123
    {
124
        // If the restoring event does not return false, we will proceed with this
125
        // restore operation. Otherwise, we bail out so the developer will stop
126
        // the restore totally. We will clear the deleted timestamp and save.
127
        if ($this->fireModelEvent('restoring') === false) {
128
            return false;
129
        }
130
 
131
        $this->{$this->getDeletedAtColumn()} = null;
132
 
133
        // Once we have saved the model, we will fire the "restored" event so this
134
        // developer will do anything they need to after a restore operation is
135
        // totally finished. Then we will return the result of the save call.
136
        $this->exists = true;
137
 
138
        $result = $this->save();
139
 
140
        $this->fireModelEvent('restored', false);
141
 
142
        return $result;
143
    }
144
 
145
    /**
146
     * Restore a soft-deleted model instance without raising any events.
147
     *
148
     * @return bool
149
     */
150
    public function restoreQuietly()
151
    {
152
        return static::withoutEvents(fn () => $this->restore());
153
    }
154
 
155
    /**
156
     * Determine if the model instance has been soft-deleted.
157
     *
158
     * @return bool
159
     */
160
    public function trashed()
161
    {
162
        return ! is_null($this->{$this->getDeletedAtColumn()});
163
    }
164
 
165
    /**
166
     * Register a "softDeleted" model event callback with the dispatcher.
167
     *
168
     * @param  \Closure|string  $callback
169
     * @return void
170
     */
171
    public static function softDeleted($callback)
172
    {
173
        static::registerModelEvent('trashed', $callback);
174
    }
175
 
176
    /**
177
     * Register a "restoring" model event callback with the dispatcher.
178
     *
179
     * @param  \Closure|string  $callback
180
     * @return void
181
     */
182
    public static function restoring($callback)
183
    {
184
        static::registerModelEvent('restoring', $callback);
185
    }
186
 
187
    /**
188
     * Register a "restored" model event callback with the dispatcher.
189
     *
190
     * @param  \Closure|string  $callback
191
     * @return void
192
     */
193
    public static function restored($callback)
194
    {
195
        static::registerModelEvent('restored', $callback);
196
    }
197
 
198
    /**
399 lars 199
     * Register a "forceDeleting" model event callback with the dispatcher.
200
     *
201
     * @param  \Closure|string  $callback
202
     * @return void
203
     */
204
    public static function forceDeleting($callback)
205
    {
206
        static::registerModelEvent('forceDeleting', $callback);
207
    }
208
 
209
    /**
148 lars 210
     * Register a "forceDeleted" model event callback with the dispatcher.
211
     *
212
     * @param  \Closure|string  $callback
213
     * @return void
214
     */
215
    public static function forceDeleted($callback)
216
    {
217
        static::registerModelEvent('forceDeleted', $callback);
218
    }
219
 
220
    /**
221
     * Determine if the model is currently force deleting.
222
     *
223
     * @return bool
224
     */
225
    public function isForceDeleting()
226
    {
227
        return $this->forceDeleting;
228
    }
229
 
230
    /**
231
     * Get the name of the "deleted at" column.
232
     *
233
     * @return string
234
     */
235
    public function getDeletedAtColumn()
236
    {
237
        return defined(static::class.'::DELETED_AT') ? static::DELETED_AT : 'deleted_at';
238
    }
239
 
240
    /**
241
     * Get the fully qualified "deleted at" column.
242
     *
243
     * @return string
244
     */
245
    public function getQualifiedDeletedAtColumn()
246
    {
247
        return $this->qualifyColumn($this->getDeletedAtColumn());
248
    }
249
}