检索旧的复选框数组

时间:2016-05-04 20:27:38

标签: laravel laravel-5 laravel-5.1 laravel-5.2

我有一个复选框数组,其值在数据库中存储为JSON,我想获取数据库中的旧值或存储值。这是我写的代码。

它没有按照我的预期运作,是否有最佳方法来处理这个问题?

<?php $savedDevices = json_decode(Auth::user()->devices, true);?>
<input type="checkbox" name="devices[Android Phone]" value="1" {{(old('devices') !== null && array_key_exists("Android Tablet", old('devices')) ||array_key_exists("Android Tablet", $savedDevices) )?"checked":""}}> Android Phone </label>
<input type="checkbox" name="devices[Android Tablet]" value="1"  {{(old('devices') !== null && array_key_exists("Android Tablet", old('devices')) ||array_key_exists("Android Tablet", $savedDevices) )?"checked":""}}> Android Tablet </label>
<input type="checkbox" name="devices[iPhone]" value="1" {{(old('devices') !== null && array_key_exists("iPhone", old('devices')) ||array_key_exists("iPhone", $savedDevices) )?"checked":""}}> iPhone </label>
<input type="checkbox" name="devices[iPad]" value="1" {{(old('devices') !== null && array_key_exists("iPad", old('devices')) ||array_key_exists("iPad", $savedDevices) )?"checked":""}}> iPad </label>

5 个答案:

答案 0 :(得分:0)

如果您的解码json可能如下所示

Insert into TB_LN_CASES (col1, col2,col3,col4,
                    ...
                    ...col129,col130) Values (abc,efd,gr,y,t,ui,u,re,re    

根据上面的$ savedDevices数据,前两个复选框被选中,因为值= yes和iphone复选框未选中,因为它的值不等于是。

现在,如果您想更改手机和平板电脑的值,请取消选中复选框并提交值

答案 1 :(得分:0)

当您使用withInput($ request-&gt; all())时,可以在表单中使用old。 在您的控制器中

$validator = \Validator::make($data, Distributors::rules($request->id));
if (!$validator->passes()) {
            return redirect()->back()->withInput($request->all())->withErrors($validator->errors());
    }

表格

$savedDevices = [
  'phone'  =>'yes',
  'tablet' => 'yes',
   'iphone' => 'no'
                   ] 

<label>
           Phone :<input type="checkbox"
        {{(old('phone') == 'yes') ? "checked":""  }} name="phone"> 
  Tablet :<input type="checkbox"
            {{(old('tablet') == "yes") ? "checked":""  }} name="tablet">
 Iphone :<input type="checkbox"
            {{(old('iphone') == "yes") ? "checked":""  }} name="iphone">
    </label>

注意:旧版(&#39;你的名字复选框必须使用&#39;)

答案 2 :(得分:0)

  1. - &gt;首先,当您点击编辑更新表单加载时。
  2. - &gt;然后在加载表单后,旧输入为空,因此检查数据库值
  3. - &gt;提交表单后,如果验证失败,则重定向回旧表单
  4.   Phone :<input type="checkbox"
                  @if(old('phone') !=null)
                {{(old('phone') == 'on') ? "checked":""  }}
                  @else
               {{($savedDevices->tablet == "on") ? "checked":""  }}
        @endif
         name="phone">
    

答案 3 :(得分:0)

这对我有用

    <?php $savedDevices = json_decode(Auth::user()->devices, true);?>
                            {{$checked = ""}}
                            <!-- Check if it is a post back-->
                            <?php $postBack = old()?>
                            @if(empty($postBack))
                                <!-- First Request, get data from the DB -->
                                @if(array_key_exists('Android Phone',$savedDevices))
                                    <?php $checked = "checked";?>
                                @endif
                            @else
                                 <!-- Update attempt has happend -->
                                @if(array_key_exists('Android Phone',old('devices',[])))
                                    <?php $checked = "checked";?>
                                @else
                                    <?php $checked = "";?>
                                @endif
                            @endif
                        <label>
                            <input type="checkbox" name="devices[Android Phone]" value="1" {{$checked}} > Android Phone </label>

答案 4 :(得分:-1)

试试这个

<?php $old_devices = old('devices', []); ?>

<input type="checkbox" name="devices[Android Phone]" value="1" {{array_key_exists("Android Tablet", $old_devices) || array_key_exists("Android Tablet", $savedDevices) ? "checked":""}}> Android Phone </label>
相关问题