protected function getRelatedModels(Model $model)
    {
        // Store the class name
        $modelClass = get_class($model);

        // Check if we already know the related models for this model
        if (!isset($this->relatedModels[$modelClass]))
        {
            $relatedModels = [];

            $methods = with(new \ReflectionClass($model))->getMethods();

            // Iterate all class methods
            foreach ($methods as $method)
            {
                // Check if this method returns an Eloquent relation
                if ($method->class == $modelClass &&
                    preg_match('/@return\s+\\\\Illuminate\\\\Database\\\\Eloquent\\\\Relations/', $method->getDocComment())
                )
                {
                    // Get the method name, so that we can call it on the model
                    $relationMethod = $method->name;

                    // Find the relation
                    $relation = $model->$relationMethod();

                    if ($relation instanceof Relation)
                    {
                        // Find the related model
                        $related = $relation->getRelated();

                        // Store the method to help build the inverse path
                        $this->relationClassMethods[$modelClass][get_class($related)] = $relationMethod;

                        $relatedModels[] = ['model' => $related, 'method' => $method];
                    }
                }
            }

            // Cache related models for this model
            $this->relatedModels[$modelClass] = $relatedModels;

            // Return the related models
            return $relatedModels;
        } else
        {
            // Return from cache
            return $this->relatedModels[$modelClass];
        }
    }
protected function getRelatedModels(Model $model)
    {
        // Store the class name
        $modelClass = get_class($model);

        // Check if we already know the related models for this model
        if (!isset($this->relatedModels[$modelClass]))
        {
            $relatedModels = [];

            $methods = with(new \ReflectionClass($model))->getMethods();

            // Iterate all class methods
            foreach ($methods as $method)
            {
                // Check if this method returns an Eloquent relation
                if ($method->class == $modelClass &&
                    preg_match('/@return\s+\\\\Illuminate\\\\Database\\\\Eloquent\\\\Relations/', $method->getDocComment())
                )
                {
                    // Get the method name, so that we can call it on the model
                    $relationMethod = $method->name;

                    // Find the relation
                    $relation = $model->$relationMethod();

                    if ($relation instanceof Relation)
                    {
                        // Find the related model
                        $related = $relation->getRelated();

                        // Store the method to help build the inverse path
                        $this->relationClassMethods[$modelClass][get_class($related)] = $relationMethod;

                        $relatedModels[] = ['model' => $related, 'method' => $method];
                    }
                }
            }

            // Cache related models for this model
            $this->relatedModels[$modelClass] = $relatedModels;

            // Return the related models
            return $relatedModels;
        } else
        {
            // Return from cache
            return $this->relatedModels[$modelClass];
        }
    }
protected function getRelatedModels ( Model $ model ) { // Store the class name $ modelClass = get_class ( $ model ) ; // Check if we already know the related models for this model if ( ! isset ( $ this -> relatedModels [ $ modelClass ] ) ) { $ relatedModels = [ ] ; $ methods = with ( new \ ReflectionClass ( $ model ) ) -> getMethods ( ) ; // Iterate all class methods foreach ( $ methods as $ method ) { // Check if this method returns an Eloquent relation if ( $ method -> class == $ modelClass && preg_match ( '/@return\s+\\\\Illuminate\\\\Database\\\\Eloquent\\\\Relations/' , $ method -> getDocComment ( ) ) ) { // Get the method name, so that we can call it on the model $ relationMethod = $ method -> name ; // Find the relation $ relation = $ model -> $ relationMethod ( ) ; if ( $ relation instanceof Relation ) { // Find the related model $ related = $ relation -> getRelated ( ) ; // Store the method to help build the inverse path $ this -> relationClassMethods [ $ modelClass ] [ get_class ( $ related ) ] = $ relationMethod ; $ relatedModels [ ] = [ 'model' => $ related , 'method' => $ method ] ; } } } // Cache related models for this model $ this -> relatedModels [ $ modelClass ] = $ relatedModels ; // Return the related models return $ relatedModels ; } else { // Return from cache return $ this -> relatedModels [ $ modelClass ] ; } }
Find related models from a base model

@param $model
@return array
Find related models from a base model