skip to Main Content

Following, I have a code for displaying a list of packages. Data is gathered from a config file and I checked the data, it fits. If statement is needed to determine if the packages has $package->monthlyPrice and $package->yearly (isset()). Since only packages has these are paid ones, I check for if key == free
The component works fine provided there is no if statement. But as soon as I added the following if statement, it brakes and gives me foollowing error:
syntax error, unexpected ‘endforeach’ (T_ENDFOREACH), expecting elseif (T_ELSEIF) or else (T_ELSE) or endif (T_ENDIF)

I use laravel 8.*

<x-container>
    <x-slot name="overheading">
        {{$data->overheading}}
    </x-slot>
    <x-slot name="h1">
        {{$data->h1}}
    </x-slot>
    <x-slot name="content">
       <div class="row">
            @foreach($data->list as $key => $package)
                @if($key == 'free')
                    <x-rectangle-price class="{{$key}}" :name="$package->name" :link="$package->link">
                @else
                    <x-rectangle-price class="{{$key}}" :yearly="$package->yearly" :monthlyprice="$package->monthlyPrice" :name="$package->name" :link="$package->link">
                @endif
                    <x-slot name="title"></x-slot>
                    <x-slot name="feats">
                        @foreach($package->package_feats as $feat)
                            <li class="{{$feat->active}}"> {!!$feat->content!!} </li>
                        @endforeach
                    </x-slot>
                </x-rectangle-price>
            @endforeach
       </div>
    </x-slot>
</x-container>

Normally this type of if statement works fine with plain html components. Laravel 8 components however has this issue. Last time I solved it via adding the unset variables to the config as null however It is not a good practice. @isset is also not working

2

Answers


  1. I dont think you can use @if statement to conditionate the opening of an x component.
    You should reword your code to avoid it

    <x-container>
        <x-slot name="overheading">
            {{$data->overheading}}
        </x-slot>
        <x-slot name="h1">
            {{$data->h1}}
        </x-slot>
        <x-slot name="content">
            <div class="row">
                @foreach($data->list as $key => $package)
                    @if($key == 'free')
                        <x-rectangle-price class="{{$key}}" :name="$package->name" :link="$package->link">
                            <x-slot name="title"></x-slot>
                            <x-slot name="feats">
                                @foreach($package->package_feats as $feat)
                                    <li class="{{$feat->active}}"> {!!$feat->content!!} </li>
                                @endforeach
                            </x-slot>
                        </x-rectangle-price>
                    @else
                        <x-rectangle-price class="{{$key}}" :yearly="$package->yearly" :monthlyprice="$package->monthlyPrice" :name="$package->name" :link="$package->link">
                            <x-slot name="title"></x-slot>
                            <x-slot name="feats">
                                @foreach($package->package_feats as $feat)
                                    <li class="{{$feat->active}}"> {!!$feat->content!!} </li>
                                @endforeach
                            </x-slot>
                        </x-rectangle-price>
                    @endif
                @endforeach
            </div>
        </x-slot>
    </x-container>
    
    Login or Signup to reply.
  2. Missed {{ }} in statement, not problem of @if or @isset.

    @if($key == 'free')
        <x-rectangle-price class="{{$key}}" :name="{{$package->name}}" :link="{{$package->link}}">
    @else
        <x-rectangle-price class="{{$key}}" :yearly="{{$package->yearly}}" :monthlyprice="{{$package->monthlyPrice}}" :name="{{$package->name}}" :link="{{$package->link}}">
    @endif
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search