protected function mergeForeignKeysWithAttributes(array $foreignKeys, array $attributes): array
    {
        $cachedAttributes = $this->getCachedRawAttributes();

        foreach ($foreignKeys as $fkAttributeKey => $fkAttributeValue) {
            // FK doesn't exist in attributes => we set it
            if (!array_key_exists($fkAttributeKey, $attributes)) {
                $attributes[$fkAttributeKey] = $fkAttributeValue;
                continue;
            }

            // FK does exists in attributes and is equal => we set it
            if ($attributes[$fkAttributeKey] === $fkAttributeValue) {
                $attributes[$fkAttributeKey] = $fkAttributeValue;
                continue;
            }

            // ForeignKey exists in attributes array, but the value is different that
            // the one fetched from the relationship itself.

            // Does it exist in cache
            if (array_key_exists($fkAttributeKey, $cachedAttributes)) {
                // attribute is different than cached value, we use it
                if ($attributes[$fkAttributeKey] !== $cachedAttributes[$fkAttributeKey]) {
                    continue;
                } else { // if not, we use the foreign key value
                    $attributes[$fkAttributeKey] = $fkAttributeValue;
                }
            } else {
                if (is_null($attributes[$fkAttributeKey])) {
                    $attributes[$fkAttributeKey] = $fkAttributeValue;
                }
            }
        }

        return $attributes;
    }