1
<?php
2

3
namespace Nuwave\Lighthouse\SoftDeletes;
4

5
use Illuminate\Database\Eloquent\Builder as EloquentBuilder;
6
use Illuminate\Database\Eloquent\Relations\Relation;
7
use Laravel\Scout\Builder as ScoutBuilder;
8
use Nuwave\Lighthouse\Schema\Directives\BaseDirective;
9
use Nuwave\Lighthouse\Support\Contracts\ArgBuilderDirective;
10

11
class TrashedDirective extends BaseDirective implements ArgBuilderDirective
12
{
13
    public const MODEL_MUST_USE_SOFT_DELETES = 'Use @trashed only for Model classes that use the SoftDeletes trait.';
14

15 1
    public static function definition(): string
16
    {
17
        return /** @lang GraphQL */ <<<'GRAPHQL'
18 1
"""
19
Allows to filter if trashed elements should be fetched.
20
"""
21
directive @trashed on ARGUMENT_DEFINITION | INPUT_FIELD_DEFINITION
22
GRAPHQL;
23
    }
24

25
    /**
26
     * Apply withTrashed, onlyTrashed or withoutTrashed to given $builder if needed.
27
     *
28
     * @param string|null $value "with", "without" or "only"
29
     */
30 1
    public function handleBuilder($builder, $value): object
31
    {
32 1
        if ($builder instanceof Relation) {
33 1
            $model = $builder->getRelated();
34 1
        } elseif ($builder instanceof ScoutBuilder) {
35 0
            $model = $builder->model;
36 1
        } elseif ($builder instanceof EloquentBuilder) {
37 1
            $model = $builder->getModel();
38
        } else {
39 0
            throw new \Exception('Can not get model from builder of class: '.get_class($builder));
40
        }
41

42 1
        SoftDeletesServiceProvider::assertModelUsesSoftDeletes(
43 1
            get_class($model),
44 1
            self::MODEL_MUST_USE_SOFT_DELETES
45
        );
46

47 1
        if (! isset($value)) {
48 0
            return $builder;
49
        }
50

51 1
        $trashedModificationMethod = "{$value}Trashed";
52 1
        $builder->{$trashedModificationMethod}();
53

54 1
        return $builder;
55
    }
56
}

Read our documentation on viewing source code .

Loading