【问题标题】:Laravel Array to Input checkbox true or false valueLaravel Array to Input 复选框 true 或 false 值
【发布时间】:2021-04-09 02:35:03
【问题描述】:

您好,我有一行数据,我希望允许用户单击复选框打开/关闭数据以供使用。我已经阅读了https://stackoverflow.com/a/43995087/14936674 的答案,它工作正常,除了我的复选框值只有在被选中时才能更新。如果我想取消选中该框并对其进行更新,则不会对其进行更新。目前我已经通过更新复选框进行了测试,但是它会更新并取消选中我的所有数据。

下面是我的blade.php

<tbody>
@foreach($operatinghrs as $i => $operatinghr)
<tr id="{{ $operatinghr->{'opID'} }}">
<td>
<input type="hidden" name="operatinghrs[{{ $i }}][opID]" value="{{ $operatinghr->{'opID'} }}">{{ $operatinghr->{'day'} }}
</td>                                    
<td>
<input type="time" id="start_time" name="operatinghrs[{{ $i }}][start_time]" min="00:00" max="23:59" value="{{  display24HTime(old('start_time', $operatinghr->start_time))}}">
</td>
<td>
<input type="time" id="end_time" name="operatinghrs[{{ $i }}][end_time]"  min="00:00" max="23:59" value="{{ display24HTime(old('end_time', $operatinghr->end_time))}}">
</td>
<td>
<div class="switch"> 
<input type="checkbox" id="clinic_open" name="operatinghrs[{{ $i }}][clinic_open]" class="switch-input" value="1" {{ old('clinic_open', $operatinghr->clinic_open=="true") ? 'checked="checked"' : '' }}/>
<div class="circle"></div>
</div> 
</td>   
</tr>
@endforeach
</tbody>

以下是我的控制器类中的代码:

 public function update(Request $request)
    {
        foreach($request->get('operatinghrs', []) as $operatinghr) {
            $db = new OperatingHour();
            $db->where('opID', $operatinghr['opID'])->where('clinic_id', '=', $_SESSION['clinic_ID'])
                ->update(
                    [
                    'clinic_open' => $request->input('clinic_open') ? true : false, 
                    
                ]
            );
        }

        return back()->with('success_message', 'Details updated successfully!');
    }

【问题讨论】:

    标签: php laravel laravel-8


    【解决方案1】:

    如果您想仅更新 $operatinghr['clinic_open'] 值为 true 的情况。

    考虑来自 MDN 的 this:

    如果在提交表单时未选中复选框,则没有值提交给服务器来表示其未选中状态(例如 value=unchecked);该值根本不提交给服务器。如果您想在未选中时为复选框提交默认值,您可以在表单中包含一个具有相同名称和值的表单,可能由 JavaScript 生成。

    对于特定条目,您还必须以$operatinghr['clinic_open'] 的形式访问clinic_open 上的$operatinghr 值。不像$request-&gt;input('clinic_open')

    所以解决方案必须是这样的:

    // Get all Operating values.
    $operatinghrs = $request->get('operatinghrs', []);
    
    /* 
     * Filter values with clinic_open criteria.  
     * Checkbox values are only present when is checked. 
     * So isset control work for us.
     */ 
    $operatingsForUpdate = array_filter($operatinghrs, function($op){
        return isset($op['clinic_open']); // return only has clinic_open value.
    });
    
    // Loop for only for update operatings.
    foreach($operatingsForUpdate  as $operatinghr) {
         // Update specific entry which contain clinic_open value is true.           
    }
    
    
    
    

    【讨论】:

    • 您好,感谢您的意见!如果我想更新 Clinic_open 值为 false 的条目怎么办?正如我遇到的情况,用户可能希望在以前为 true 时将 Clinic_open 值设置为 false
    • 您不能过滤已发布的$operatinghrs 并为每个项目循环。之后,在循环内部,您必须从 db 获取当前操作时间。并检查clinic_open isset 条件并检查特定项目当前(来自数据库)clinic_open 值。如果clinic_open 不存在且当前项目值为真。将项目值设为 false。
    • 嗨,我在我的 foreach 方法中做了一个 if else 循环:foreach($request-&gt;get('operatinghrs', []) as $operatinghr) { if(array_has($operatinghr, ['clinic_open'])){ $db = new OperatingHour(); $db-&gt;where('opID', $operatinghr['opID'])-&gt;where('clinic_id', $_SESSION['clinic_ID'])-&gt;update(array_except($operatinghr, ['opID'])); } else{ $db = new OperatingHour(); $db-&gt;where('opID', $operatinghr['opID'])-&gt;where('clinic_id', $_SESSION['clinic_ID']) -&gt;update([ 'start_time' =&gt; $operatinghr['start_time'], 'end_time' =&gt; $operatinghr['end_time'], 'clinic_open' =&gt; false, ] ); } } 它正在工作
    • 看起来不错。我认为您可以在循环外通过one query with posted Ids 获取当前项目。并且您可以将当前项目与发布的值同步以避免性能问题。
    【解决方案2】:

    使用复选框总是很棘手,复选框的默认值为on 和off,但如果未选中复选框,它将不会出现在请求数据中。

    因此,您需要在尝试更新之前验证请求中是否存在复选框,如果不存在,则在数据库中保存字符串“关闭”或不保存任何内容并为其设置默认值(“关闭”)字段,当您在编辑/更新表单中检索数据时,所有带有“关闭”值的复选框都将被取消选中。

    在$request 上使用has 方法,如下所示:

    if($request->has('clinic_open')) {
        foreach($request->get('operatinghrs', []) as $operatinghr) {
          $db = new OperatingHour();
          $db->where('opID', $operatinghr['opID'])->where('clinic_id', $_SESSION['clinic_ID'])
                    ->update(['clinic_open' => $request->input('clinic_open')]);
            }  
          }
    

    一些建议:
    避免在 where 方法上使用 '=' 条件,laravel 对其进行总结,节省时间和空间。
    去掉复选框的value='1',把一些工作留给浏览器,少做。

    【讨论】:

    • 您好,感谢您的意见!我已经尝试了您的建议,但是在我的 dd() 输出中的 if($request-&gt;has('clinic_open')) 中,即使我检查了一些复选框,它也返回 false。我的数据库中的 Clinic_open 值也是布尔值,它的真或假
    猜你喜欢
    • 2014-06-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-06-03
    • 1970-01-01
    • 2012-01-09
    • 2015-02-26
    相关资源
    最近更新 更多