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 MassPrunable
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
        $query = tap($this->prunable(), function ($query) use ($chunkSize) {
19
            $query->when(! $query->getQuery()->limit, function ($query) use ($chunkSize) {
20
                $query->limit($chunkSize);
21
            });
22
        });
23
 
24
        $total = 0;
25
 
26
        do {
27
            $total += $count = in_array(SoftDeletes::class, class_uses_recursive(get_class($this)))
28
                        ? $query->forceDelete()
29
                        : $query->delete();
30
 
31
            if ($count > 0) {
32
                event(new ModelsPruned(static::class, $total));
33
            }
34
        } while ($count > 0);
35
 
36
        return $total;
37
    }
38
 
39
    /**
40
     * Get the prunable model query.
41
     *
42
     * @return \Illuminate\Database\Eloquent\Builder
43
     */
44
    public function prunable()
45
    {
46
        throw new LogicException('Please implement the prunable method on your model.');
47
    }
48
}