Subversion-Projekte lars-tiefland.laravel_shop

Revision

Zur aktuellen Revision | Details | 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
    {
48
        $this->forceDeleting = true;
49
 
50
        return tap($this->delete(), function ($deleted) {
51
            $this->forceDeleting = false;
52
 
53
            if ($deleted) {
54
                $this->fireModelEvent('forceDeleted', false);
55
            }
56
        });
57
    }
58
 
59
    /**
60
     * Perform the actual delete query on this model instance.
61
     *
62
     * @return mixed
63
     */
64
    protected function performDeleteOnModel()
65
    {
66
        if ($this->forceDeleting) {
67
            return tap($this->setKeysForSaveQuery($this->newModelQuery())->forceDelete(), function () {
68
                $this->exists = false;
69
            });
70
        }
71
 
72
        return $this->runSoftDelete();
73
    }
74
 
75
    /**
76
     * Perform the actual delete query on this model instance.
77
     *
78
     * @return void
79
     */
80
    protected function runSoftDelete()
81
    {
82
        $query = $this->setKeysForSaveQuery($this->newModelQuery());
83
 
84
        $time = $this->freshTimestamp();
85
 
86
        $columns = [$this->getDeletedAtColumn() => $this->fromDateTime($time)];
87
 
88
        $this->{$this->getDeletedAtColumn()} = $time;
89
 
90
        if ($this->usesTimestamps() && ! is_null($this->getUpdatedAtColumn())) {
91
            $this->{$this->getUpdatedAtColumn()} = $time;
92
 
93
            $columns[$this->getUpdatedAtColumn()] = $this->fromDateTime($time);
94
        }
95
 
96
        $query->update($columns);
97
 
98
        $this->syncOriginalAttributes(array_keys($columns));
99
 
100
        $this->fireModelEvent('trashed', false);
101
    }
102
 
103
    /**
104
     * Restore a soft-deleted model instance.
105
     *
106
     * @return bool
107
     */
108
    public function restore()
109
    {
110
        // If the restoring event does not return false, we will proceed with this
111
        // restore operation. Otherwise, we bail out so the developer will stop
112
        // the restore totally. We will clear the deleted timestamp and save.
113
        if ($this->fireModelEvent('restoring') === false) {
114
            return false;
115
        }
116
 
117
        $this->{$this->getDeletedAtColumn()} = null;
118
 
119
        // Once we have saved the model, we will fire the "restored" event so this
120
        // developer will do anything they need to after a restore operation is
121
        // totally finished. Then we will return the result of the save call.
122
        $this->exists = true;
123
 
124
        $result = $this->save();
125
 
126
        $this->fireModelEvent('restored', false);
127
 
128
        return $result;
129
    }
130
 
131
    /**
132
     * Restore a soft-deleted model instance without raising any events.
133
     *
134
     * @return bool
135
     */
136
    public function restoreQuietly()
137
    {
138
        return static::withoutEvents(fn () => $this->restore());
139
    }
140
 
141
    /**
142
     * Determine if the model instance has been soft-deleted.
143
     *
144
     * @return bool
145
     */
146
    public function trashed()
147
    {
148
        return ! is_null($this->{$this->getDeletedAtColumn()});
149
    }
150
 
151
    /**
152
     * Register a "softDeleted" model event callback with the dispatcher.
153
     *
154
     * @param  \Closure|string  $callback
155
     * @return void
156
     */
157
    public static function softDeleted($callback)
158
    {
159
        static::registerModelEvent('trashed', $callback);
160
    }
161
 
162
    /**
163
     * Register a "restoring" model event callback with the dispatcher.
164
     *
165
     * @param  \Closure|string  $callback
166
     * @return void
167
     */
168
    public static function restoring($callback)
169
    {
170
        static::registerModelEvent('restoring', $callback);
171
    }
172
 
173
    /**
174
     * Register a "restored" model event callback with the dispatcher.
175
     *
176
     * @param  \Closure|string  $callback
177
     * @return void
178
     */
179
    public static function restored($callback)
180
    {
181
        static::registerModelEvent('restored', $callback);
182
    }
183
 
184
    /**
185
     * Register a "forceDeleted" model event callback with the dispatcher.
186
     *
187
     * @param  \Closure|string  $callback
188
     * @return void
189
     */
190
    public static function forceDeleted($callback)
191
    {
192
        static::registerModelEvent('forceDeleted', $callback);
193
    }
194
 
195
    /**
196
     * Determine if the model is currently force deleting.
197
     *
198
     * @return bool
199
     */
200
    public function isForceDeleting()
201
    {
202
        return $this->forceDeleting;
203
    }
204
 
205
    /**
206
     * Get the name of the "deleted at" column.
207
     *
208
     * @return string
209
     */
210
    public function getDeletedAtColumn()
211
    {
212
        return defined(static::class.'::DELETED_AT') ? static::DELETED_AT : 'deleted_at';
213
    }
214
 
215
    /**
216
     * Get the fully qualified "deleted at" column.
217
     *
218
     * @return string
219
     */
220
    public function getQualifiedDeletedAtColumn()
221
    {
222
        return $this->qualifyColumn($this->getDeletedAtColumn());
223
    }
224
}