Completed
Push — master ( d5af7a...83c102 )
by
unknown
02:50 queued 01:21
created

FiltersTrashed::__invoke()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 16
rs 9.7333
c 0
b 0
f 0
cc 3
nc 3
nop 3
1
<?php
2
3
namespace Spatie\QueryBuilder\Filters;
4
5
use Illuminate\Database\Eloquent\Builder;
6
7
/**
8
 * FiltersTrashed provides filter for soft deleted (trashed) records.
9
 *
10
 * This filter responds to particular values:
11
 *
12
 * - 'with' - include 'trashed' records to the result set.
13
 * - 'only' - return only 'trashed' records at the result set.
14
 * - any other - return only records without 'trashed' at the result set.
15
 *
16
 * Usage example:
17
 *
18
 * ```php
19
 * QueryBuilder::for(Item::class)
20
 *     ->allowedFilters([
21
 *         AllowedFilter::trashed(),
22
 *         // ...
23
 *     ]);
24
 * ```
25
 *
26
 * @see \Illuminate\Database\Eloquent\SoftDeletes
27
 * @see \Spatie\QueryBuilder\AllowedFilter::trashed()
28
 */
29
class FiltersTrashed implements Filter
30
{
31
    /** {@inheritdoc} */
32
    public function __invoke(Builder $query, $value, string $property)
33
    {
34
        if ($value === 'with') {
35
            $query->withTrashed();
36
37
            return;
38
        }
39
40
        if ($value === 'only') {
41
            $query->onlyTrashed();
42
43
            return;
44
        }
45
46
        $query->withoutTrashed();
47
    }
48
}
49