One solution could be to use string interpolation to replace the variables in the clauses with their corresponding values from the $covenant object. Here's an example implementation:
// Assuming $covenant is an instance of the Covenant model with the necessary properties
// Retrieve the clauses for the covenant
$clauses = $covenant->clauses;
// Loop through each clause and replace the variables with their values
foreach ($clauses as $clause) {
// Use preg_replace_callback to replace each variable with its value
$clause->text = preg_replace_callback('/$(\w+)/', function ($matches) use ($covenant) {
// Get the variable name (without the $)
$variableName = $matches[1];
// Check if the variable exists as a property on the $covenant object
if (property_exists($covenant, $variableName)) {
// If it does, return its value
return $covenant->$variableName;
} else {
// If it doesn't, return the original variable string
return $matches[0];
}
}, $clause->text);
}
// Now $clauses contains the updated clauses with the variables replaced with their values
This implementation uses a regular expression to match variables in the clause text (e.g. $price, $owner, $date) and then uses preg_replace_callback to replace each variable with its corresponding value from the $covenant object. If a variable is not found as a property on the object, it is left unchanged.