From f39badd01dc793c7ccec3a2fc31fee9554cef990 Mon Sep 17 00:00:00 2001 From: Chris Morrell Date: Mon, 13 Sep 2021 14:43:43 -0400 Subject: [PATCH] [8.x] Add forwardDecoratedCallTo (#38800) * [8.x] Add forwardDecoratedCallTo * Code style * Update ForwardsCalls.php Co-authored-by: Taylor Otwell --- .../Database/Eloquent/Relations/Relation.php | 8 +------ .../Support/Traits/ForwardsCalls.php | 21 +++++++++++++++++++ 2 files changed, 22 insertions(+), 7 deletions(-) diff --git a/src/Illuminate/Database/Eloquent/Relations/Relation.php b/src/Illuminate/Database/Eloquent/Relations/Relation.php index 38dc76395f0b..94efb8f48b87 100644 --- a/src/Illuminate/Database/Eloquent/Relations/Relation.php +++ b/src/Illuminate/Database/Eloquent/Relations/Relation.php @@ -489,13 +489,7 @@ public function __call($method, $parameters) return $this->macroCall($method, $parameters); } - $result = $this->forwardCallTo($this->query, $method, $parameters); - - if ($result === $this->query) { - return $this; - } - - return $result; + return $this->forwardDecoratedCallTo($this->query, $method, $parameters); } /** diff --git a/src/Illuminate/Support/Traits/ForwardsCalls.php b/src/Illuminate/Support/Traits/ForwardsCalls.php index 48ca2f878cea..204b6a4bdae5 100644 --- a/src/Illuminate/Support/Traits/ForwardsCalls.php +++ b/src/Illuminate/Support/Traits/ForwardsCalls.php @@ -41,6 +41,27 @@ protected function forwardCallTo($object, $method, $parameters) } } + /** + * Forward a method call to the given object, returning $this if the forwarded call returned itself. + * + * @param mixed $object + * @param string $method + * @param array $parameters + * @return mixed + * + * @throws \BadMethodCallException + */ + protected function forwardDecoratedCallTo($object, $method, $parameters) + { + $result = $this->forwardCallTo($object, $method, $parameters); + + if ($result === $object) { + return $this; + } + + return $result; + } + /** * Throw a bad method call exception for the given method. *