1
<?php
2

3
namespace Nuwave\Lighthouse\Execution\Arguments;
4

5
use Illuminate\Database\Eloquent\Relations\Relation;
6
use Nuwave\Lighthouse\Support\Contracts\ArgResolver;
7

8
class NestedOneToMany implements ArgResolver
9
{
10
    /**
11
     * @var string
12
     */
13
    protected $relationName;
14

15 1
    public function __construct(string $relationName)
16
    {
17 1
        $this->relationName = $relationName;
18
    }
19

20
    /**
21
     * @param  \Illuminate\Database\Eloquent\Model  $parent
22
     * @param  \Nuwave\Lighthouse\Execution\Arguments\ArgumentSet  $args
23
     */
24 1
    public function __invoke($parent, $args): void
25
    {
26
        /** @var \Illuminate\Database\Eloquent\Relations\HasMany|\Illuminate\Database\Eloquent\Relations\MorphMany $relation */
27 1
        $relation = $parent->{$this->relationName}();
28

29 1
        static::createUpdateUpsert($args, $relation);
30

31 1
        if ($args->has('delete')) {
32 1
            $relation->getRelated()::destroy(
33 1
                $args->arguments['delete']->toPlain()
34
            );
35
        }
36
    }
37

38
    /**
39
     * @param  \Nuwave\Lighthouse\Execution\Arguments\ArgumentSet  $args
40
     */
41 1
    public static function createUpdateUpsert(ArgumentSet $args, Relation $relation): void
42
    {
43 1
        if ($args->has('create')) {
44 1
            $saveModel = new ResolveNested(new SaveModel($relation));
45

46 1
            foreach ($args->arguments['create']->value as $childArgs) {
47
                // @phpstan-ignore-next-line Relation&Builder mixin not recognized
48 1
                $saveModel($relation->make(), $childArgs);
49
            }
50
        }
51

52 1
        if ($args->has('update')) {
53 1
            $updateModel = new ResolveNested(new UpdateModel(new SaveModel($relation)));
54

55 1
            foreach ($args->arguments['update']->value as $childArgs) {
56
                // @phpstan-ignore-next-line Relation&Builder mixin not recognized
57 1
                $updateModel($relation->make(), $childArgs);
58
            }
59
        }
60

61 1
        if ($args->has('upsert')) {
62 1
            $upsertModel = new ResolveNested(new UpsertModel(new SaveModel($relation)));
63

64 1
            foreach ($args->arguments['upsert']->value as $childArgs) {
65
                // @phpstan-ignore-next-line Relation&Builder mixin not recognized
66 1
                $upsertModel($relation->make(), $childArgs);
67
            }
68
        }
69
    }
70
}

Read our documentation on viewing source code .

Loading