Refine Model Attribute Access in TemplateService: Enhanced the replacePlaceholders method to improve the retrieval of model attributes and accessors. Updated logic to handle image/file URLs more effectively and streamlined the process of accessing model properties, ensuring robustness in placeholder replacement.

This commit is contained in:
Ümit Tunç
2025-11-06 16:52:40 -03:00
parent f65ba75f41
commit 1fd56cc953
+12 -5
View File
@@ -525,28 +525,35 @@ class TemplateService
} }
} }
// 4. If value not found in data and model is provided, try to get from model attributes // 4. If value not found in data and model is provided, try to get from model attributes/accessors
if ($value === null && $model && count($parts) === 2) { if ($value === null && $model && count($parts) === 2) {
[$type, $field] = $parts; [$type, $field] = $parts;
// First, try to get accessor with _url suffix for image/file fields (e.g., featured_image_url) // First, try to get accessor with _url suffix for image/file fields (e.g., featured_image_url)
if (in_array($type, ['image', 'images', 'file', 'files'])) { if (in_array($type, ['image', 'images', 'file', 'files'])) {
$accessorField = $field . '_url'; $accessorField = $field . '_url';
if ($model->offsetExists($accessorField) || isset($model->{$accessorField})) {
try { try {
// Laravel accessors are called as properties
if (isset($model->{$accessorField})) {
$value = $model->{$accessorField}; $value = $model->{$accessorField};
}
} catch (\Exception $e) { } catch (\Exception $e) {
// Accessor doesn't exist, continue to try attribute // Accessor doesn't exist, continue to try attribute
} }
} }
}
// Try to get from model attribute (e.g., featured_image, title, content) // Try to get from model attribute (e.g., featured_image, title, content)
if ($value === null && ($model->offsetExists($field) || isset($model->{$field}))) { if ($value === null) {
try { try {
// Try as property first (may trigger accessor)
if (isset($model->{$field})) {
$value = $model->{$field};
} else {
// Try getAttribute directly
$value = $model->getAttribute($field); $value = $model->getAttribute($field);
}
} catch (\Exception $e) { } catch (\Exception $e) {
// Attribute doesn't exist // Attribute doesn't exist, value stays null
} }
} }
} }