-
-
Notifications
You must be signed in to change notification settings - Fork 468
Expand file tree
/
Copy pathSimpleModelsLoader.php
More file actions
31 lines (24 loc) · 916 Bytes
/
SimpleModelsLoader.php
File metadata and controls
31 lines (24 loc) · 916 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
<?php declare(strict_types=1);
namespace Nuwave\Lighthouse\Execution\ModelsLoader;
use Illuminate\Database\Eloquent\Collection as EloquentCollection;
use Illuminate\Database\Eloquent\Model;
class SimpleModelsLoader implements ModelsLoader
{
public function __construct(
protected string $relation,
protected \Closure $decorateBuilder,
) {}
public function load(EloquentCollection $parents): void
{
$parents->loadMissing([$this->relation => $this->decorateBuilder]);
}
public function extract(Model $model): mixed
{
// Dot notation may be used to eager load nested relations
$parts = explode('.', $this->relation);
// We just return the first level of relations for now.
// They hold the nested relations in case they are needed.
$firstRelation = $parts[0];
return $model->getRelation($firstRelation);
}
}