From 7cb1ff67b7cba591394760e6743710abe8959377 Mon Sep 17 00:00:00 2001 From: Liam Hammett Date: Mon, 30 Dec 2019 01:45:10 +0000 Subject: [PATCH] Add mixin support to Eloquent Builder --- src/Illuminate/Database/Eloquent/Builder.php | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/src/Illuminate/Database/Eloquent/Builder.php b/src/Illuminate/Database/Eloquent/Builder.php index dd23abf8fa51..f0d1998774ca 100755 --- a/src/Illuminate/Database/Eloquent/Builder.php +++ b/src/Illuminate/Database/Eloquent/Builder.php @@ -13,6 +13,8 @@ use Illuminate\Support\Arr; use Illuminate\Support\Str; use Illuminate\Support\Traits\ForwardsCalls; +use ReflectionClass; +use ReflectionMethod; /** * @property-read HigherOrderBuilderProxy $orWhere @@ -1372,6 +1374,24 @@ public static function __callStatic($method, $parameters) return; } + if ($method === 'mixin') { + $mixin = $parameters[0]; + $replace = $parameters[1] ?? true; + + $methods = (new ReflectionClass($mixin))->getMethods( + ReflectionMethod::IS_PUBLIC | ReflectionMethod::IS_PROTECTED + ); + + foreach ($methods as $method) { + if ($replace || ! static::hasMacro($method->name)) { + $method->setAccessible(true); + static::macro($method->name, $method->invoke($mixin)); + } + } + + return; + } + if (! static::hasGlobalMacro($method)) { static::throwBadMethodCallException($method); }