diff --git a/src/Core/Schema/Concerns/EagerLoadable.php b/src/Core/Schema/Concerns/EagerLoadable.php index 4024e98..3eee64c 100644 --- a/src/Core/Schema/Concerns/EagerLoadable.php +++ b/src/Core/Schema/Concerns/EagerLoadable.php @@ -19,24 +19,40 @@ namespace LaravelJsonApi\Core\Schema\Concerns; +use Closure; +use InvalidArgumentException; + trait EagerLoadable { /** - * @var bool + * @var Closure|bool */ private bool $includePath = true; /** + * @param Closure|bool $callback * @return $this */ - public function cannotEagerLoad(): self + public function canEagerLoad($callback = true): self { - $this->includePath = false; + if (!is_bool($callback) && !$callback instanceof Closure) { + throw new InvalidArgumentException('Expecting a boolean or closure.'); + } + + $this->includePath = $callback; return $this; } + /** + * @return $this + */ + public function cannotEagerLoad(): self + { + return $this->canEagerLoad(false); + } + /** * Is the relation allowed as an include path? * @@ -44,6 +60,10 @@ public function cannotEagerLoad(): self */ public function isIncludePath(): bool { + if ($this->includePath instanceof Closure) { + $this->includePath = ($this->includePath)(); + } + return $this->includePath; } }