使用 Blade 模板以 Laravel 5 形式预选选项的条件 @if 语句

Conditional @if statement for preselected options in Laravel 5 form using Blade templates

在使用与创建表单相同的模板创建更新表单时,我希望选择之前选择的选项。像这样的东西是有道理的,但 Blade 无法解析它:

{!! Form::select('number', [1, 2, 3, 4, 5] @if (!empty($at)), $at->id @endif) !!}

我使用 $at 变量来确定我是在更新还是创建新条目。

我可以编写 2 个单独的语句并回显相关的语句(或使用单独的模板,但我试图让代码保持干爽。

blade 标签内的代码很正常 PHP。这也意味着您可以在以下情况下使用 shorthand 内联:

{!! Form::select('number', [1, 2, 3, 4, 5], (empty($at) ? null : $at->id)) !!}