如何使用不同的名称保存多个复选框? (Laravel)

时间:2019-05-10 07:56:00

标签: php sql-server laravel laravel-5

嗨,我在保存多个具有不同名称的复选框时遇到问题 我对菜单具有案例创建权限,并且菜单具有不同的访问权限,例如创建,读取,更新,删除等

我尝试使用此代码,但结果无效

这是我的刀片模板

<android.support.constraint.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <!--<RelativeLayout-->
        <!--android:layout_width="wrap_content"-->
        <!--android:layout_height="wrap_content">-->
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginBottom="8dp"
        android:text="A"
        android:textSize="20sp"
        android:textColor="#000"
        android:textStyle="bold"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginBottom="8dp"
        android:text="SAMPLE TWO"
        android:textSize="10sp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
    <!--</RelativeLayout>-->

</android.support.constraint.ConstraintLayout>

这里是我的存储方法

<div class="table-responsive">
    <table id="bs4-table" class="table table-striped table-bordered" style="width:100%">
        <thead>
            <tr>
                <th>No</th>
                <th>Menu</th>
                <th>Create</th>
                <th>Read</th>
                <th>Update</th>
                <th>Delete</th>
                <th>Approve</th>
                <th>Export PDF</th>
                <th>Export Excel</th>
            </thead>
            <tbody>
                @foreach ($permission_menus as $permission_menu)
                   <tr>
                       <td>{{ $loop->iteration }}</td>
                       <td>
                           {{ $permission_menu->name }} <input type="checkbox" name="menus_id[]" value="{{ $permission_menu->id }}">
                       </td>
                       <td>
                           <input type="checkbox" name="create_access[]" value="0" onclick="changeFlag(this)">
                       </td>
                       <td>
                           <input type="checkbox" name="read_access[]" value="0" onclick="changeFlag(this)">
                       </td>
                       <td>
                           <input type="checkbox" name="update_access[]" value="0" onclick="changeFlag(this)">
                       </td>
                       <td>
                           <input type="checkbox" name="delete_access[]" value="0" onclick="changeFlag(this)">
                       </td>
                       <td>
                           <input type="checkbox" name="approve_access[]" value="0" onclick="changeFlag(this)">
                       </td>
                       <td>
                           <input type="checkbox" name="export_pdf_access[]" value="0" onclick="changeFlag(this)">
                       </td>
                       <td>
                           <input type="checkbox" name="export_excel_access[]" value="0" onclick="changeFlag(this)">
                       </td>
                   </tr>
               @endforeach
           </tbody>
       </table>
   </div>

有人知道如何解决此问题吗?还是有任何解决办法?

1 个答案:

答案 0 :(得分:0)

如果取消选中菜单项之一而未取消选中该菜单下的所有权限,则结果将不一致。您可以始终强制将标识符作为菜单索引:

<div class="table-responsive">
    <table id="bs4-table" class="table table-striped table-bordered" style="width:100%">
        <thead>
        <tr>
            <th>No</th>
            <th>Menu</th>
            <th>Create</th>


            <th>Read</th>
            <th>Update</th>
            <th>Delete</th>
            <th>Approve</th>
            <th>Export PDF</th>
            <th>Export Excel</th>
        </tr>
        </thead>
        <tbody>
        @foreach ($permission_menus as $permission_menu)
        <tr>
            <td>{{ $loop->iteration }}</td>
            <td>
                {{ $permission_menu->name }} <input type="checkbox" name="menus_id[]"
                                                    value="{{ $permission_menu->id }}">
            </td>
            <td>
                <input type="checkbox" name="create_access[{{$permission_menu->id}}]" value="0" onclick="changeFlag(this)">
            </td>
            <td>
                <input type="checkbox" name="read_access[{{$permission_menu->id}}]" value="0" onclick="changeFlag(this)">
            </td>
            <td>
                <input type="checkbox" name="update_access[{{$permission_menu->id}}]" value="0" onclick="changeFlag(this)">
            </td>
            <td>
                <input type="checkbox" name="delete_access[{{$permission_menu->id}}]" value="0" onclick="changeFlag(this)">
            </td>
            <td>
                <input type="checkbox" name="approve_access[{{$permission_menu->id}}]" value="0" onclick="changeFlag(this)">
            </td>
            <td>
                <input type="checkbox" name="export_pdf_access[{{$permission_menu->id}}]" value="0"
                       onclick="changeFlag(this)">
            </td>
            <td>
                <input type="checkbox" name="export_excel_access[{{$permission_menu->id}}]" value="0"
                       onclick="changeFlag(this)">
            </td>
        </tr>
        @endforeach
        </tbody>
    </thead>
</table>

这样,您可以正确匹配结果:


public function store(Request $request)
{
        $this->validate($request, [
            'user_access' => 'required',
        ], [
            'user_access.required' => 'User harus dipilih!',
        ]);

        foreach($request->menus_id as $menu_id)
        {
            $create_access = $request->create_access;
            $read_access = $request->read_access;
            $update_access = $request->update_access;
            $delete_access = $request->delete_access;
            $approve_access = $request->approve_access;
            $export_pdf_access = $request->export_pdf_access;
            $export_excel_access = $request->export_excel_access;


            if (!isset($create_access[$menu_id])) {
                $create_access[$menu_id] = 0;
            }

            if(!isset($read_access[$menu_id])){
                $read_access[$menu_id] = 0;
            }

            if(!isset($update_access[$menu_id])){
                $update_access[$menu_id] = 0;
            }

            if(!isset($delete_access[$menu_id])){
                $delete_access[$menu_id] = 0;
            }

            if(!isset($approve_access[$index])){
                $approve_access[$index] = 0;
            }

            if(!isset($export_pdf_access[$menu_id])){
                $export_pdf_access[$menu_id] = 0;
            }

            if(!isset($export_excel_access[$menu_id])){
                $export_excel_access[$menu_id] = 0;
            }



            $permission = [
                'menu_id' => $menu_id,

                'create_access' => $create_access[$menu_id],       
               'read_access' => $read_access[$menu_id],
                'update_access' => $update_access[$menu_id],
                'delete_access' => $delete_access[$menu_id],
                'approve_access' => $approve_access[$menu_id],
                'export_pdf_access' => $export_pdf_access[$menu_id],
                'export_excel_access' => $export_excel_access[$menu_id],
                'user_id' => $request->user_id,
                'user_access' => $request->user_access
            ];


            DB::table('permissions')->insert($permission);
        }

        return redirect()->back()->with('success', 'Hak akses berhasil di set!');
    }

注意:除了执行if(!isset($x[$menu_id])) { $x[$menu_id] = 0; }之外,您还可以在$x[$menu_id]??0数组中执行$permission