diff --git a/framework/core/src/Core/Models/Model.php b/framework/core/src/Core/Models/Model.php index 7412858cb..fa6a7e7a7 100755 --- a/framework/core/src/Core/Models/Model.php +++ b/framework/core/src/Core/Models/Model.php @@ -24,6 +24,13 @@ class Model extends Eloquent */ protected static $rules = []; + /** + * The custom relations on this model, registered by extensions. + * + * @var array + */ + protected static $relationships = []; + /** * The forum model instance. * @@ -185,4 +192,34 @@ class Model extends Eloquent throw new PermissionDeniedException; } } + + /** + * Add a custom relationship to the model. + * + * @param string $name The name of the relationship. + * @param Closure $callback The callback to execute. This should return an + * Eloquent relationship object. + * @return void + */ + public static function addRelationship($name, $callback) + { + static::$relationships[$name] = $callback; + } + + /** + * Check for and execute custom relationships. + * + * @param string $name + * @param array $arguments + * @return mixed + */ + public function __call($name, $arguments) + { + if (isset(static::$relationships[$name])) { + array_unshift($arguments, $this); + return call_user_func_array(static::$relationships[$name], $arguments); + } + + return parent::__call($name, $arguments); + } }