如何将所有复选框存储在laravel中?

时间:2018-09-30 14:05:38

标签: laravel-5.4

我的管理面板上有64个复选框,因此只有网站管理员可以选择或取消它们。

如何在laravel中存储所有复选框?

我需要保存在我的桌子上选中的复选框,如下所示:

create.blade.php

<table class="table table-bordered">
    @foreach($infractions as $infraction)
        <tr>
            <th>{{ $infraction->title }}</th>
            <td>
                <input type="hidden" name="infractions[{{ $infraction->id }}]" value="0" >
                <input type="checkbox" value="1" name="infractions[{{ $infraction->id }}]">
            </td>
        </tr>
    @endforeach
</table>
<br>
<table class="table table-bordered">
    @foreach($encouragements as $encouragement)
        <tr>
            <th class="translate">{{ $encouragement->title }}</th>
            <td>
                <div class="input-group">
                    <input type="text" id="num{{$encouragement->id}}" value="0" class="form-control col-sm-1" name="encouragement[{{ $encouragement->id }}]">
                    <div class="input-group-btn">
                            <input type="button" onclick="incrementValue({{$encouragement->id}})" class="btn btn-warning" value="Add">
                    </div>
                </div>
            </td>
        </tr>
    @endforeach
</table>

InspectionController.php

public function store(Request $request)
{
    $infraction_data="";
    foreach ($request->infractions as $key => $val)
    {
        $infraction_data.=$key.'='.$val.'|';
    }
    $infraction_data=substr($infraction_data,0,strlen($infraction_data)-1);
    $encouragement_data="";
    foreach ($request->encouragement as $key => $val)
    {
        $encouragement_data.=$key.'='.$val.'|';
    }
    $encouragement_data=substr($encouragement_data,0,strlen($encouragement_data)-1);

    $inspection = new Inspection();
    $inspection->infraction_data = $infraction_data;
    $inspection->encouragement_data = $encouragement_data;
    $inspection->user_id = auth()->user()->id;
    $inspection->requisition_id = 1;
    $inspection->save();
}

0 个答案:

没有答案