public static function loadRelationsAttributesRecursive(Model $model)
{
$attributes = $model->getAttributes();
foreach ($attributes as $key => $value) {
if (method_exists($model, $key)) {
$reflection_method = new ReflectionMethod($model, $key);
if ($reflection_method->class != "Illuminate\\Database\\Eloquent\\Model") {
$relation = $model->{$key}();
if ($relation instanceof Relation) {
// Check if the relation field is single model or collections
if (is_null($value) === true || !static::isMultiLevelArray($value)) {
$value = [$value];
}
$models = static::hydrateRecursive($relation->getModel(), $value, $relation);
// Unset attribute before match relation
unset($model[$key]);
$relation->match([$model], $models, $key);
}
}
}
}
}