Subversion-Projekte lars-tiefland.laravel_shop

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
148 lars 1
<?php
2
 
3
namespace Illuminate\Database\Eloquent;
4
 
5
use Illuminate\Database\Events\ModelsPruned;
6
use LogicException;
7
 
8
trait Prunable
9
{
10
    /**
11
     * Prune all prunable models in the database.
12
     *
13
     * @param  int  $chunkSize
14
     * @return int
15
     */
16
    public function pruneAll(int $chunkSize = 1000)
17
    {
18
        $total = 0;
19
 
20
        $this->prunable()
21
            ->when(in_array(SoftDeletes::class, class_uses_recursive(get_class($this))), function ($query) {
22
                $query->withTrashed();
23
            })->chunkById($chunkSize, function ($models) use (&$total) {
24
                $models->each->prune();
25
 
26
                $total += $models->count();
27
 
28
                event(new ModelsPruned(static::class, $total));
29
            });
30
 
31
        return $total;
32
    }
33
 
34
    /**
35
     * Get the prunable model query.
36
     *
37
     * @return \Illuminate\Database\Eloquent\Builder
38
     */
39
    public function prunable()
40
    {
41
        throw new LogicException('Please implement the prunable method on your model.');
42
    }
43
 
44
    /**
45
     * Prune the model in the database.
46
     *
47
     * @return bool|null
48
     */
49
    public function prune()
50
    {
51
        $this->pruning();
52
 
53
        return in_array(SoftDeletes::class, class_uses_recursive(get_class($this)))
54
                ? $this->forceDelete()
55
                : $this->delete();
56
    }
57
 
58
    /**
59
     * Prepare the model for pruning.
60
     *
61
     * @return void
62
     */
63
    protected function pruning()
64
    {
65
        //
66
    }
67
}