Skip to content

Commit 404e791

Browse files
Thedigittimgws
authored andcommitted
Replaced method_exists() with is_callable() (#62)
1 parent 2c88375 commit 404e791

File tree

1 file changed

+19
-16
lines changed

1 file changed

+19
-16
lines changed

src/ElasticquentTrait.php

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Elasticquent;
44

55
use Exception;
6+
use ReflectionMethod;
67
use Illuminate\Database\Eloquent\Model;
78
use Illuminate\Database\Eloquent\Relations\Relation;
89

@@ -685,25 +686,27 @@ public static function hydrateRecursive(Model $model, array $items, Relation $pa
685686
* @param \Illuminate\Database\Eloquent\Model $model
686687
*/
687688
public static function loadRelationsAttributesRecursive(Model $model)
688-
{
689-
$attributes = $model->getAttributes();
690-
691-
foreach ($attributes as $key => $value) {
692-
if (method_exists($model, $key)) {
693-
$relation = $model->$key();
694-
if ($relation instanceof Relation) {
695-
// Check if the relation field is single model or collections
696-
if (!static::isMultiLevelArray($value)) {
697-
$value = [$value];
698-
}
699-
$models = static::hydrateRecursive($relation->getModel(), $value, $relation);
700-
// Unset attribute before match relation
701-
unset($model[$key]);
702-
$relation->match([$model], $models, $key);
703-
}
689+
{
690+
$attributes = $model->getAttributes();
691+
foreach ($attributes as $key => $value) {
692+
if (method_exists($model, $key) ) {
693+
$refl = new ReflectionMethod($model, $key);
694+
if($refl->class != "Illuminate\Database\Eloquent\Model"){
695+
$relation = $model->$key();
696+
if ($relation instanceof Relation) {
697+
// Check if the relation field is single model or collections
698+
if (!static::isMultiLevelArray($value)) {
699+
$value = [$value];
704700
}
701+
$models = static::hydrateRecursive($relation->getModel(), $value, $relation);
702+
// Unset attribute before match relation
703+
unset($model[$key]);
704+
$relation->match([$model], $models, $key);
705+
}
705706
}
707+
}
706708
}
709+
}
707710

708711
/**
709712
* Get the pivot attribute from a model.

0 commit comments

Comments
 (0)