压缩 Laravel blade if(old()) 语句

Condense Laravel blade if(old()) statement down

我的 create 视图中有以下工作代码

<div class="flex justify-between">
  <label class="inline-flex items-center">
    <input name="contact_preferences[]" @if(old('contact_preferences') && in_array('post', old('contact_preferences'))) checked @endif value="post" type="checkbox" class="form-checkbox"><span class="ml-2 text-gray-700">Post</span>
  </label>
  <label class="inline-flex items-center">
    <input name="contact_preferences[]" @if(old('contact_preferences') && in_array('email', old('contact_preferences'))) checked @endif value="email" type="checkbox" class="form-checkbox"><span class="ml-2 text-gray-700">E-Mail</span>
  </label>            
  <label class="inline-flex items-center">
    <input name="contact_preferences[]" @if(old('contact_preferences') && in_array('phone', old('contact_preferences'))) checked @endif value="phone" type="checkbox" class="form-checkbox"><span class="ml-2 text-gray-700">Phone</span>
  </label>
  <label class="inline-flex items-center">
    <input name="contact_preferences[]" @if(old('contact_preferences') && in_array('none', old('contact_preferences'))) checked @endif value="none" type="checkbox" class="form-checkbox"><span class="ml-2 text-gray-700">None</span>
  </label>
</div>

如您所见,我使用复选框来标记用户所做的选择。但是我的 if 语句在其他验证规则失败时将这些复选框显示为 checked 有点让我震惊;它有效,但似乎不必要地长。

是否有一些我完全不知道可以使用的刀刃魔法来缩短语句?

您可以创建函数,这样它会更多 'dry':

 <div class="flex justify-between">
        <?php
            function isChecked($name){
                return old('contact_preferences') && in_array($name, old('contact_preferences')) ? 'checked': '';
            }
        ?>
        <label class="inline-flex items-center">
            <input name="contact_preferences[]" {{isChecked('post')}} value="post" type="checkbox" class="form-checkbox"><span class="ml-2 text-gray-700">Post</span>
        </label>
        <label class="inline-flex items-center">
            <input name="contact_preferences[]" {{isChecked('email')}} value="email" type="checkbox" class="form-checkbox"><span class="ml-2 text-gray-700">E-Mail</span>
        </label>
        <label class="inline-flex items-center">
            <input name="contact_preferences[]" {{isChecked('phone')}} value="phone" type="checkbox" class="form-checkbox"><span class="ml-2 text-gray-700">Phone</span>
        </label>
        <label class="inline-flex items-center">
            <input name="contact_preferences[]" {{isChecked('none')}} value="none" type="checkbox" class="form-checkbox"><span class="ml-2 text-gray-700">None</span>
        </label>
    </div>

您应该能够通过使用 null coalesce 而不是检查条目是否存在来缩短条件:

<div class="flex justify-between">
  <label class="inline-flex items-center">
    <input name="contact_preferences[]" @if(in_array('post', old('contact_preferences') ?? [])) checked @endif value="post" type="checkbox" class="form-checkbox"><span class="ml-2 text-gray-700">Post</span>
  </label>
</div>

还有另一个选择——我实际上并不知道您可以像这样在一行中内联 @if/@endif。我经常做的是一个简单的 ternary and echo 像这样:

<div class="flex justify-between">
  <label class="inline-flex items-center">
    <input name="contact_preferences[]" {{ in_array('post', old('contact_preferences') ?? []) ? "checked" : "" }} value="post" type="checkbox" class="form-checkbox"><span class="ml-2 text-gray-700">Post</span>
  </label>
</div>