Php 如果(old())语句已关闭,请压缩Laravel刀片
我的Php 如果(old())语句已关闭,请压缩Laravel刀片,php,laravel,laravel-blade,Php,Laravel,Laravel Blade,我的create视图中有以下工作代码 <div class="flex justify-between"> <label class="inline-flex items-center"> <input name="contact_preferences[]" @if(old('contact_preferences') && in_array('post', old('cont
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
,这让我的大脑有点不舒服;它是有效的,但似乎太长了
是否有一些我不知道的刀片魔法可以缩短语句?您可以创建函数,使其更“干燥”:
<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>
邮递
电子邮件
电话
没有一个
您应该能够通过使用而不是检查条目的存在来缩短您的条件:
<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>
但是在视图中不应该这样做,所以这不是一个很好的解决方案Sadly我不认为这是最好的答案我会试一试,但是,我的部分问题是,如果在其他地方验证失败,
contact_preferences
将是null
,因此无法在_array中检查数组内部。这就是为什么null coalesce opah,哦,是的,让我试试这2秒。@如果(in_array('post',old('contact_preferences')??[])checked@endif
工作正常,谢谢:)
<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>