From 9443d145fab08428ce3a9f12478f9049e21cd93e Mon Sep 17 00:00:00 2001 From: blmage Date: Mon, 19 Oct 2020 21:19:20 +0200 Subject: [PATCH 1/2] Only load includes which correspond to actual relationships (fix #1096) --- src/Transformer/Adapter/Fractal.php | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/src/Transformer/Adapter/Fractal.php b/src/Transformer/Adapter/Fractal.php index 74d890a60..ebc2f6de8 100644 --- a/src/Transformer/Adapter/Fractal.php +++ b/src/Transformer/Adapter/Fractal.php @@ -95,6 +95,19 @@ public function transform($response, $transformer, Binding $binding, Request $re $eagerLoads = array_intersect($eagerLoads, $transformer->getAvailableIncludes()); } + if (!empty($eagerLoads)) { + // Only load includes which correspond to actual relationships + $item = $response->first(); + + if ($item) { + foreach ($eagerLoads as $key => $eagerLoad) { + if (!method_exists($item, $eagerLoad)) { + unset($eagerLoads[$key]); + } + } + } + } + $response->load($eagerLoads); } From 00a9e7ab18b9c371849198dbd6266ea4dc078b12 Mon Sep 17 00:00:00 2001 From: blmage Date: Mon, 19 Oct 2020 21:41:52 +0200 Subject: [PATCH 2/2] Fix CI --- src/Transformer/Adapter/Fractal.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Transformer/Adapter/Fractal.php b/src/Transformer/Adapter/Fractal.php index ebc2f6de8..959eaa5b1 100644 --- a/src/Transformer/Adapter/Fractal.php +++ b/src/Transformer/Adapter/Fractal.php @@ -95,13 +95,13 @@ public function transform($response, $transformer, Binding $binding, Request $re $eagerLoads = array_intersect($eagerLoads, $transformer->getAvailableIncludes()); } - if (!empty($eagerLoads)) { + if (! empty($eagerLoads)) { // Only load includes which correspond to actual relationships $item = $response->first(); if ($item) { foreach ($eagerLoads as $key => $eagerLoad) { - if (!method_exists($item, $eagerLoad)) { + if (! method_exists($item, $eagerLoad)) { unset($eagerLoads[$key]); } }