[英]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()
函数中尝试的方法是:
zone_id
is matched删除数据透视表中zone_id
匹配的行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':''}} >
{{ $hol->holiday_name }}
</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.