Laravel: Conditional rendering with dynamic components not working

  laravel, laravel-blade

I want to have a dynamic component that can conditionally render child dynamic components.

Let’s imagine having a parent component Parent.php

In my view blade this is then called something like this

(Of course, this wouldn’t be parent but a method that resolves that name)

<x-dynamic-component :elements="$elements" component="parent" />

Within that component, based on the $elements variable, it should render other dynamic components

$element could be for instance Radio.php or Checkbox.php

So what I’m trying to do within my parent component is this:

@foreach($elements as $element)
        <x-component-dynamic :component="$element->getComponentName()" />

The problem is, that Laravel nevertheless parses those lines regardless of this if statement.

I’m getting the following error message:

Unable to locate a class or view for component [component-dynamic]. (View: …..)

This is because $element->hasDynamicComponent() is false and therefore getComponentName is empty. Of course Laravel cannot resolve a component without a name.

The thing is Laravel shouldn’t even try to resolve this, because hasDynamicComponent is false.

Source: Laravel

Leave a Reply