简体   繁体   English

选中/取消选中复选框值时更新数据透视表

[英]Update pivot table when checked/unchecked checkbox value

I have a form containing checkboxes that have values/lists of holiday names.我有一个包含具有假日名称值/列表的复选框的表单。

The situation is a many-to-many relationship where One ZONE can apply One/Many HOLIDAY and One HOLIDAY can be applied by One/Many ZONE.这种情况是一个多对多的关系,一个 ZONE 可以应用 One/Many HOLIDAY,One HOLIDAY 可以应用 One/Many ZONE。

So, there is a pivot table named Zone_Holidays :因此,有一个名为Zone_Holidays的数据透视表:

----------------------- 
Zone_ID  |  Holiday_ID
-----------------------   
   1     |      3    
   1     |      2    
   2     |      1    
   2     |      2    
   2     |      3    
   3     |      2
-----------------------

The RESULT of PIVOT table comes out when HOLIDAY is CHECKED according to zone.根据区域检查 HOLIDAY 时,PIVOT 表的结果出来了。

For the first time input, there's no problem.第一次输入,没有问题。 The problem is, when I want to UPDATE holiday for zone by clicking (checked/unchecked) on the checkbox, there is no change.问题是,当我想通过单击(选中/未选中)复选框来更新区域的假期时,没有任何变化。

The way I have tried in update() function on controller is:我在控制器上的update()函数中尝试的方法是:

  • delete rows in pivot table where zone_id is matched删除数据透视表中zone_id匹配的行
  • then insert again然后再次插入

In blade,在刀片中,

<?php
$zone_id = $dataTypeContent->getKey();
$holidays = Holiday::select('id','holiday_name')->get();
$zone_holiday = ZoneHoliday::where('zone_id', $zone_id)->pluck('holiday_id')->toArray();
?>

<div class="form-group">
    @foreach ($holidays as $hol)

     <div class="col-md-4">
         <input type="checkbox" value="{{$hol->id}}" name="holidays[]" {{in_array($hol->id,$zone_holiday)?'checked':''}} >  
         &nbsp;{{ $hol->holiday_name }}&nbsp;
      </div>

    @endforeach
</div>

In controller,在控制器中,

public function _update(Request $request, $id) {
    $holidays = $request->input('holidays');

    if(!empty($holidays))
    { 
       $delete_holidays = ZoneHoliday::where('zone_id', $id)->delete(); //failed at this state

       if($delete_holidays) 
       {
           for($count = 0; $count < count($holidays); $count++)
           {
             $data2 = array(
                  'holiday_id' => $holidays[$count],
                  'zone_id'  => $id,
                );
                $insert_holiday[] = $data2;   
           }
            ZoneHoliday::insert($insert_holiday);
        }
    }
}

The result of this, nothing is deleted in the pivot table, but controller received the request of holiday and id's of zone.这样做的结果是,数据透视表中没有任何内容被删除,但控制器收到了假期和区域 ID 的请求。

What you seem to be looking for is the sync method that you will also find in the laravel docs https://laravel.com/docs/5.8/eloquent-relationships#updating-many-to-many-relationships您似乎正在寻找的是sync方法,您也可以在 laravel 文档中找到https://laravel.com/docs/5.8/eloquent-relationships#updating-many-to-many-relationships

public function _update(Request $request, $id) {
  // I'd expect this to be an array of IDs e.g. [1,2,3]
  $holidays = $request->input('holidays');
  $zone = Zone::find('id', $id);
  $zone->holidays()->sync($holidays);
  ...
}

This will ofcourse only work if you setup your realtions accordingly.这当然只有在您相应地设置您的房地产时才有效。 https://laravel.com/docs/5.8/eloquent-relationships#many-to-many https://laravel.com/docs/5.8/eloquent-relationships#many-to-many

It seems you currently have a model for the relation - if it only contains the two IDs I don't think you actually want / need it, as you can query everything relevant via the relations directly.看来您目前有一个关系模型 - 如果它只包含两个 ID,我认为您实际上不需要/不需要它,因为您可以直接通过关系查询所有相关内容。 If you have a lot of pivot attributes it might be different.如果您有很多枢轴属性,则可能会有所不同。

Use following code使用以下代码

public function _update(Request $request, $zone_id) 
{
     $zone = Zone::find($zone_id);
     $zone->holidays()->sync($request->holidays);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用 codeigniter 将选中和未选中复选框的值更新为表 - How to update the value of checked and unchecked checkbox to table using codeigniter 从复选框获取值 - 未选中时为 0,选中时为 1 - Get value from checkbox - 0 when unchecked, 1 when checked 复选框基于显示值被选中还是未选中? - checkbox checked or unchecked based on the value Display? 在数据库中插入值复选框。 未选中=0 选中=1 - Insert value checkbox in database. Unchecked=0 checked=1 如果在 html 表中选中复选框,那么我想在按下更新按钮时更新该行的值 - if checkbox is checked in html table then i want to update the value of that row when update button is pressed 当复选框被选中或复选框未选中删除值时,如何在另一个文本框中获得复选框值和文本框值的总和 - how can i get sum of checkbox value and textbox value in another textbox when checkbox is checked or if checkbox unchecked remove the value 复选框未选中 var 值 0 复选框已选中 var 值 1 - checkbox unchecked var value 0 checkbox checked var value 1 取消选中时发送复选框的值 - Send a value for a checkbox when it is unchecked 保持复选框处于选中状态(或未选中) - Keep checkbox checked (or) unchecked 在我的表{AJAX}中选中复选框后如何使数据库更新值 - How do I make my database update a value when a checkbox is checked in my table { AJAX }
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM