diff --git a/src/Illuminate/View/Compilers/ComponentTagCompiler.php b/src/Illuminate/View/Compilers/ComponentTagCompiler.php index e4dcabf61262..f73fd77c0f4c 100644 --- a/src/Illuminate/View/Compilers/ComponentTagCompiler.php +++ b/src/Illuminate/View/Compilers/ComponentTagCompiler.php @@ -203,8 +203,7 @@ protected function componentString(string $component, array $attributes) $parameters = $data->all(); } - return " @component('{$class}', [".$this->attributesToString($parameters, $escapeBound = false).']) -withName(\''.$component.'\'); ?> + return " @component('{$class}', '{$component}', [".$this->attributesToString($parameters, $escapeBound = false).']) withAttributes(['.$this->attributesToString($attributes->all()).']); ?>'; } diff --git a/src/Illuminate/View/Compilers/Concerns/CompilesComponents.php b/src/Illuminate/View/Compilers/Concerns/CompilesComponents.php index 7fc998721330..a9f9d7e7c7ea 100644 --- a/src/Illuminate/View/Compilers/Concerns/CompilesComponents.php +++ b/src/Illuminate/View/Compilers/Concerns/CompilesComponents.php @@ -21,16 +21,16 @@ trait CompilesComponents */ protected function compileComponent($expression) { - [$component, $data] = strpos($expression, ',') !== false - ? array_map('trim', explode(',', trim($expression, '()'), 2)) - : [trim($expression, '()'), '']; + [$component, $alias, $data] = strpos($expression, ',') !== false + ? array_map('trim', explode(',', trim($expression, '()'), 3)) + ['', '', ''] + : [trim($expression, '()'), '', '']; $component = trim($component, '\'"'); $hash = static::newComponentHash($component); if (Str::contains($component, ['::class', '\\'])) { - return static::compileClassComponentOpening($component, $data, $hash); + return static::compileClassComponentOpening($component, $data, $hash, $alias); } return "startComponent{$expression}; ?>"; @@ -53,15 +53,17 @@ public static function newComponentHash(string $component) * Compile a class component opening. * * @param string $component + * @param string $alias * @param string $data * @param string $hash * @return string */ - public static function compileClassComponentOpening(string $component, string $data, string $hash) + public static function compileClassComponentOpening(string $component, string $data, string $hash, string $alias) { return implode("\n", [ '', 'getContainer()->make('.Str::finish($component, '::class').', '.($data ?: '[]').'); ?>', + 'withName('.$alias.'); ?>', 'shouldRender()): ?>', 'startComponent($component->resolveView(), $component->data()); ?>', ]); diff --git a/tests/View/Blade/BladeComponentTagCompilerTest.php b/tests/View/Blade/BladeComponentTagCompilerTest.php index f74008ef5ee1..4ed6bd89ac4b 100644 --- a/tests/View/Blade/BladeComponentTagCompilerTest.php +++ b/tests/View/Blade/BladeComponentTagCompilerTest.php @@ -32,11 +32,9 @@ public function testBasicComponentParsing() $result = $this->compiler(['alert' => TestAlertComponent::class])->compileTags('