public static function getCustomFields($findResults, $getById = false)
    {
        if (is_array($findResults)) {
            if (count($findResults) == 1 && $getById) {
                $findResults = [$findResults];
            }
        }

        $results = [];

        $classReflection = (new \ReflectionClass($findResults[0]));
        $className = $classReflection->getShortName();
        $classNamespace = $classReflection->getNamespaceName();

        $module = Modules::findFirstByName($className);

        if ($module) {
            $model = $classNamespace . '\\' . ucfirst($module->name) . 'CustomFields';
            $modelId = strtolower($module->name) . '_id';

            $customFields = CustomFields::findByModulesId($module->id);

            if (is_array($findResults)) {
                $findResults[0] = $findResults[0]->toArray();
            } else {
                $findResults = $findResults->toArray();
            }
            foreach ($findResults as $result) {
                foreach ($customFields as $customField) {
                    $result['custom_fields'][$customField->name] = '';
                    $values = [];
                    $moduleValues = $model::find([
                        $modelId . ' = ?0 AND custom_fields_id = ?1',
                        'bind' => [$result['id'], $customField->id]
                    ]);

                    if ($moduleValues->count()) {
                        $result['custom_fields'][$customField->name] = $moduleValues[0]->value;
                    }
                }

                $results[] = $result;
            }

            return $getById ? $results[0] : $results;
        }

        return $getById ? $findResults[0]->toArray() : $findResults;
    }
public static function getCustomFields($findResults, $getById = false)
    {
        if (is_array($findResults)) {
            if (count($findResults) == 1 && $getById) {
                $findResults = [$findResults];
            }
        }

        $results = [];

        $classReflection = (new \ReflectionClass($findResults[0]));
        $className = $classReflection->getShortName();
        $classNamespace = $classReflection->getNamespaceName();

        $module = Modules::findFirstByName($className);

        if ($module) {
            $model = $classNamespace . '\\' . ucfirst($module->name) . 'CustomFields';
            $modelId = strtolower($module->name) . '_id';

            $customFields = CustomFields::findByModulesId($module->id);

            if (is_array($findResults)) {
                $findResults[0] = $findResults[0]->toArray();
            } else {
                $findResults = $findResults->toArray();
            }
            foreach ($findResults as $result) {
                foreach ($customFields as $customField) {
                    $result['custom_fields'][$customField->name] = '';
                    $values = [];
                    $moduleValues = $model::find([
                        $modelId . ' = ?0 AND custom_fields_id = ?1',
                        'bind' => [$result['id'], $customField->id]
                    ]);

                    if ($moduleValues->count()) {
                        $result['custom_fields'][$customField->name] = $moduleValues[0]->value;
                    }
                }

                $results[] = $result;
            }

            return $getById ? $results[0] : $results;
        }

        return $getById ? $findResults[0]->toArray() : $findResults;
    }
public static function getCustomFields ( $ findResults , $ getById = false ) { if ( is_array ( $ findResults ) ) { if ( count ( $ findResults ) == 1 && $ getById ) { $ findResults = [ $ findResults ] ; } } $ results = [ ] ; $ classReflection = ( new \ ReflectionClass ( $ findResults [ 0 ] ) ) ; $ className = $ classReflection -> getShortName ( ) ; $ classNamespace = $ classReflection -> getNamespaceName ( ) ; $ module = Modules :: findFirstByName ( $ className ) ; if ( $ module ) { $ model = $ classNamespace . '\\' . ucfirst ( $ module -> name ) . 'CustomFields' ; $ modelId = strtolower ( $ module -> name ) . '_id' ; $ customFields = CustomFields :: findByModulesId ( $ module -> id ) ; if ( is_array ( $ findResults ) ) { $ findResults [ 0 ] = $ findResults [ 0 ] -> toArray ( ) ; } else { $ findResults = $ findResults -> toArray ( ) ; } foreach ( $ findResults as $ result ) { foreach ( $ customFields as $ customField ) { $ result [ 'custom_fields' ] [ $ customField -> name ] = '' ; $ values = [ ] ; $ moduleValues = $ model :: find ( [ $ modelId . ' = ?0 AND custom_fields_id = ?1' , 'bind' => [ $ result [ 'id' ] , $ customField -> id ] ] ) ; if ( $ moduleValues -> count ( ) ) { $ result [ 'custom_fields' ] [ $ customField -> name ] = $ moduleValues [ 0 ] -> value ; } } $ results [ ] = $ result ; } return $ getById ? $ results [ 0 ] : $ results ; } return $ getById ? $ findResults [ 0 ] -> toArray ( ) : $ findResults ; }
Get a models custom fields

@param mixed $findResults

@return Array

@TODO: Made a change to return a single record when $getById is true. Keeping an eye on it.
Get a models custom fields