首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >压缩Laravel刀片if(old())语句down

压缩Laravel刀片if(old())语句down
EN

Stack Overflow用户
提问于 2020-10-09 23:28:40
回答 2查看 42关注 0票数 0

我的create视图中有以下可用的代码

代码语言:javascript
复制
<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,这让我的大脑有点不舒服;它可以工作,但似乎不必要地长。

有没有什么我不知道的魔法可以缩短这句话?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2020-10-10 00:26:01

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

代码语言:javascript
复制
<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。我总是这样做一个简单的ternaryecho

代码语言:javascript
复制
<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>
票数 1
EN

Stack Overflow用户

发布于 2020-10-09 23:48:55

你可以创建函数,这样它就会更“干燥”:

代码语言:javascript
复制
 <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>
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/64282969

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档