在 Laravel 使用 old 助手时,当 'value' 是一个数组并且 'default' 是一个基本值时,如何检查 old('value', 'default') 的值?

In Laravel using the old helper, how to check value of old('value', 'default') when 'value' is an array and 'default' is a basic value?

假设我有复选框。每个都有一个在检查时进入数组的值。

<input type="checkbox" id="card_type1" name="card_type[]" value="easy" @if(old('card_type') != NULL && in_array('easy', old('card_type')) || old('foo', $parkingLot->m_plots_can_easycard) === '1') checked @endif>
<input type="checkbox" id="card_type2" name="card_type[]" value="icash" @if(old('card_type') != NULL && in_array('icash', old('card_type')) || old('foo', $parkingLot->m_plots_can_icash20) === '1') checked @endif>
<input type="checkbox" id="card_type3" name="card_type[]" value="ipass" @if(old('card_type') != NULL && in_array('ipass', old('card_type')) || old('foo', $parkingLot->m_plots_can_ipass) === '1') checked @endif>

第一次加载表单时,我想反映数据库值。如果数据库中的值为“1”,请选中该复选框。然后我修改复选框 - 选中一些并取消选中一些 - 并提交表单并说表单提交失败。然后我想通过检查每个值是否在旧复选框数组中来反映旧值,如果是的话检查复选框。

我的问题是 old('value', 'default') 由 'value' 和 'default' 组成,我无法真正使用单独的方法来确定是否应选中该复选框。如果我这样做(如上所示),那么我不能只有 old('default') - 并在那里进行单独检查 - 因为有一个参数会使它成为 old('value').

我不确定在我的情况下该怎么做。任何指示或帮助将不胜感激。我希望我已经足够清楚地说明了我的情况。

您可以尝试以下方法

<input type="checkbox" id="card_type1" name="card_type[]" value="easy" 
    @if (in_array('easy', old('card_type', [$parkingLot->m_plots_can_easycard === '1' ? 'easy' : '']))) 
        checked 
    @endif>
<input type="checkbox" id="card_type2" name="card_type[]" value="icash" 
    @if (in_array('icash', old('card_type', [$parkingLot->m_plots_can_icash20 === '1' ? 'icash' : '']))) 
        checked 
    @endif>
<input type="checkbox" id="card_type3" name="card_type[]" value="ipass" 
    @if (in_array('ipass', old('card_type', [$parkingLot->m_plots_can_ipass === '1' ? 'ipass' : '']))) 
        checked 
    @endif>