我的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,这让我的大脑有点不舒服;它可以工作,但似乎不必要地长。
有没有什么我不知道的魔法可以缩短这句话?
发布于 2020-10-10 00:26:01
您应该能够通过使用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和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>发布于 2020-10-09 23:48:55
你可以创建函数,这样它就会更“干燥”:
<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>https://stackoverflow.com/questions/64282969
复制相似问题