Laravel刀片隐藏了foreach循环中的选择选项

时间:2018-03-31 15:02:41

标签: php laravel

您好我正试图在Laravel的刀片视图中显示/隐藏选择选项。

我将模型($ assessment)传递给视图:

[{"id":1,"user_id":1,"name":"Appointment 1","created_at":"2018-03-31 00:00:00","updated_at":"2018-03-31 00:00:00"},{"id":2,"user_id":1,"name":"Appointment 2","created_at":"2018-03-31 00:00:00","updated_at":"2018-03-31 00:00:00"}]

在视图中我得到了:

<select name="assessment" required>
                <option selected>Select...</option>
                @foreach($assessments as $assessment)

                    @if ($assessment->name == 'Appointment 1')
                        <option value="Appointment 1">Appointment 1</option>
                    @endif

                    @if ($assessment->name == 'Appointment 2')
                        <option value="Appointment 2">Appointment 2</option>
                    @endif

                    @if ($assessment->name == 'Appointment 3')
                        <option value="Appointment 3">Appointment 3</option>
                    @endif

                @endforeach
                <option value="Follow Up Phone Call">Follow Up Phone Call</option>
                <option value="Home Assessment">Home Assessment</option> -->
            </select>

如果if语句等于True,我不知道你是否可以在foreach循环中继续绕过?否则,它会从循环中多次显示值。

2 个答案:

答案 0 :(得分:1)

我们假设在控制器中,您已获得结果并将其放入$assesments变量中。您可以做的一件事是您可以定义一个新数组并将其发送到您的视图:

$assesmentNames = [];
foreach($assesments as $assesment) {
  $assesmentNames[$assesment->name] = $assesment->name;
}
return view('myview', compact('assesments', 'assesmentNames'));

现在自动重复了。然后在您的视图中执行此操作:

@foreach($assessmentNames as $assessmentName)
  <option value="{{ $assessmentName }}"> {{ $assessmentName }} </option>
@endforeach

答案 1 :(得分:0)

你可以使用&#34;继续&#34;如果你想跳过特定的迭代,在循环内的简单php标签中的语句