From d6fd8f6249fa252f8d127d805f58b52c2adb96c3 Mon Sep 17 00:00:00 2001 From: mendicm Date: Wed, 14 Sep 2016 15:31:43 +0200 Subject: [PATCH 1/6] Set the relations as loaded although the related models field is empty on the elastic document. This prevents future queries when accessing the relationship via the magic __get method. --- src/ElasticquentTrait.php | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/ElasticquentTrait.php b/src/ElasticquentTrait.php index a23e7c4..f76d2ff 100644 --- a/src/ElasticquentTrait.php +++ b/src/ElasticquentTrait.php @@ -724,6 +724,12 @@ public static function loadRelationsAttributesRecursive(Model $model) // Unset attribute before match relation unset($model[$key]); $relation->match([$model], $models, $key); + + // The match method doesn't set the relation as loaded if the $models collection is empty + // Set the relation as loaded manually and avoid future database queries + if ((count($value) == 1 && reset($value) === null) || empty($value)) { + $model->setRelation($key, empty($value) ? new Collection() : null); + } } } } From 5eb82c47df6fb4767bc4fe974a6094309ca56723 Mon Sep 17 00:00:00 2001 From: mendicm Date: Wed, 14 Sep 2016 16:43:12 +0200 Subject: [PATCH 2/6] Added missed use. --- src/ElasticquentTrait.php | 1 + 1 file changed, 1 insertion(+) diff --git a/src/ElasticquentTrait.php b/src/ElasticquentTrait.php index f76d2ff..5ca5daf 100644 --- a/src/ElasticquentTrait.php +++ b/src/ElasticquentTrait.php @@ -5,6 +5,7 @@ use Exception; use ReflectionMethod; use Illuminate\Database\Eloquent\Model; +use Illuminate\Database\Eloquent\Collection; use Illuminate\Database\Eloquent\Relations\Relation; /** From 7f7ca6803aadfce54ff6b8a73f6cc4e4924315fc Mon Sep 17 00:00:00 2001 From: mendicm Date: Fri, 16 Sep 2016 08:33:14 +0200 Subject: [PATCH 3/6] Change package name. --- composer.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/composer.json b/composer.json index a5840c7..5c02eeb 100644 --- a/composer.json +++ b/composer.json @@ -1,5 +1,5 @@ { - "name": "elasticquent/elasticquent", + "name": "mendicm/elasticquent", "type": "library", "description": "Maps Laravel Eloquent models to Elasticsearch types.", "keywords": [ From 23c9de6cabc0b1735f615fadafed3ba89b59d139 Mon Sep 17 00:00:00 2001 From: mendicm Date: Thu, 31 May 2018 13:42:23 +0200 Subject: [PATCH 4/6] WTF? --- src/ElasticquentTrait.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/ElasticquentTrait.php b/src/ElasticquentTrait.php index d206c2c..b0e0885 100644 --- a/src/ElasticquentTrait.php +++ b/src/ElasticquentTrait.php @@ -707,7 +707,7 @@ public static function loadRelationsAttributesRecursive(Model $model) $reflection_method = new ReflectionMethod($model, $key); // Check if method class has or inherits Illuminate\Database\Eloquent\Model - if(!static::isClassInClass("Illuminate\Database\Eloquent\Model", $reflection_method->class)) { + if(static::isClassInClass("Illuminate\Database\Eloquent\Model", $reflection_method->class)) { $relation = $model->$key(); if ($relation instanceof Relation) { From c68bd7b9fc66e92298bc8791b654ef266ba329f9 Mon Sep 17 00:00:00 2001 From: mendicm Date: Fri, 14 Jun 2019 08:50:27 +0200 Subject: [PATCH 5/6] Update composer.json --- composer.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/composer.json b/composer.json index a2040c6..fe94cde 100644 --- a/composer.json +++ b/composer.json @@ -20,7 +20,7 @@ "php": ">=5.4.0", "illuminate/database": "~4.2|^5", "illuminate/config": "~4.2|^5", - "nesbot/carbon": "~1.0", + "nesbot/carbon": "~1.0|^2", "elasticsearch/elasticsearch": "~6.0" }, "require-dev": { From be6aaff348517530222481205bb51fb3ff67d88b Mon Sep 17 00:00:00 2001 From: mendicm Date: Thu, 4 Aug 2022 12:53:21 +0200 Subject: [PATCH 6/6] Update ElasticquentTrait.php --- src/ElasticquentTrait.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/ElasticquentTrait.php b/src/ElasticquentTrait.php index b0e0885..df765a5 100644 --- a/src/ElasticquentTrait.php +++ b/src/ElasticquentTrait.php @@ -744,7 +744,7 @@ public static function loadPivotAttribute(Model $model, Relation $parentRelation $attributes = $model->getAttributes(); foreach ($attributes as $key => $value) { - if ($key === 'pivot') { + if ($key === 'pivot' && $parentRelation) { unset($model[$key]); $pivot = $parentRelation->newExistingPivot($value); $model->setRelation($key, $pivot);