duanlaofu4108 2017-12-19 18:43
浏览 286

更新中间表Laravel关系

I have a table that holds the 'Officiant_id' and 'awards_id'

When a user edits their profile it will display in a checkbox all the available ones and auto check the ones that are on that table.

Although, now its time to update, let's say a user unselects one, how can I update the table to delete it, or if they check a new one add that new one to the table.

@if(!empty($officiant - > useraward))
@foreach($officiant - > useraward as $arr)
@php $removeId[] = $arr - > id @endphp {
    {
        Form::checkbox('awards[]', $arr - > id, true, ['class' => ''])
    }
} {
    {
        $arr - > name
    }
} {
    !!'<br>'!!
}
@endforeach
@endif

@php
$offaward = Info::officiantAwards();
foreach($removeId as $key) {
    unset($offaward[$key]);
}

@endphp

@foreach($offaward as $k => $v) {
    {
        Form::checkbox('awards[]', $k, false)
    }
} {
    {
        $v
    }
} {
    !!'<br>'!!
}
@endforeach

if (!empty($request - > awards)) {
    foreach($request - > awards as $i => $k) {
        $awards[] = $request - > $k;
    }
}
  • 写回答

1条回答 默认 最新

  • dongxianglun5163 2017-12-19 19:01
    关注

    Get all awards as an array and call the sync() method on your $officiant.According to documentation/Syncing Associations

    You may also use the sync method to construct many-to-many associations. The sync method accepts an array of IDs to place on the intermediate table. Any IDs that are not in the given array will be removed from the intermediate table. So, after this operation is complete, only the IDs in the given array will exist in the intermediate table:

    So in your case after calling the update() or save(),

    //get all award ids
    $awards = $request->get('awards');
    
    //save or update officient action
    
    //updating relation
    $officiant->awards()->sync($awards);
    
    评论

报告相同问题?

悬赏问题

  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测