rootModel = $rootModel; $this->localRelationship = $localRelationship; } /** * Define the distant relationship that this model has. * * @param string|callable $callback Either the distant relationship name or a callback returning the local relation. * @return \FluentBooking\Framework\Database\Orm\Relations\HasManyThrough|\FluentBooking\Framework\Database\Orm\Relations\HasOneThrough * The distant relationship instance. */ public function has($callback) { if (is_string($callback)) { $callback = fn () => $this->localRelationship->getRelated()->{$callback}(); } $distantRelation = $callback($this->localRelationship->getRelated()); if ($distantRelation instanceof HasMany) { $returnedRelation = $this->rootModel->hasManyThrough( get_class($distantRelation->getRelated()), get_class($this->localRelationship->getRelated()), $this->localRelationship->getForeignKeyName(), $distantRelation->getForeignKeyName(), $this->localRelationship->getLocalKeyName(), $distantRelation->getLocalKeyName(), ); } else { $returnedRelation = $this->rootModel->hasOneThrough( get_class($distantRelation->getRelated()), get_class($this->localRelationship->getRelated()), $this->localRelationship->getForeignKeyName(), $distantRelation->getForeignKeyName(), $this->localRelationship->getLocalKeyName(), $distantRelation->getLocalKeyName(), ); } if ($this->localRelationship instanceof MorphOneOrMany) { $returnedRelation->where($this->localRelationship->getQualifiedMorphType(), $this->localRelationship->getMorphClass()); } return $returnedRelation; } /** * Handle dynamic method calls into the model. * * @param string $method * @param array $parameters * @return mixed */ public function __call($method, $parameters) { if (Str::startsWith($method, 'has')) { return $this->has(Str::of($method)->after('has')->lcfirst()->toString()); } throw new BadMethodCallException(sprintf( 'Call to undefined method %s::%s()', static::class, $method )); } }