首页 > 解决方案 > 使用 Blade 在 foreach 循环中嵌套 if 语句

问题描述

我是 Laravel 刀片的新手,并试图在循环中嵌套一个if语句。foreach我想根据表中的字段是否等于显示不同的链接Submitted。在我看来,我编写了以下代码:

         @foreach($plans as $plan)
            <tr>
                <td> {{$plan->id}}</td>
                {{-- If the Plan Submission has been submitted, the link should bring the user to the Show function which is view only.  Cannot have user editing Plan Submission after it has been submitted. --}}
                @if ({{$plan->status}}=='Submitted')
                    <td><a href="/basicinfo/{{$plan->id}}/show">Click Here</a></td>
                @else    
                    <td><a href="/basicinfo/{{$plan->id}}/edit">Click Here</a></td>
                @endif

我收到此错误:语法错误

unexpected '<'

标签: laravellaravel-blade

解决方案


改成:

     @foreach($plans as $plan)
        <tr>
            <td> {{$plan->id}}</td>
            {{-- If the Plan Submission has been submitted, the link should bring the user to the Show function which is view only.  Cannot have user editing Plan Submission after it has been submitted. --}}
            @if ($plan->status == 'Submitted')
                <td><a href="/basicinfo/{{$plan->id}}/show">Click Here</a></td>
            @else    
                <td><a href="/basicinfo/{{$plan->id}}/edit">Click Here</a></td>
            @endif

它是@if ({{$plan->status}}=='Submitted')

不需要{{ }}内部 if 语句:)

@if ({{$plan->status}}=='Submitted')所以总的变化是: @if ($plan->status == 'Submitted')


推荐阅读