mercredi 7 août 2019

How Can I Save Checkbox Value coming form the form and save the checkbox in different record

I have a project of expense management System in which i have some checkbox and i want save that each check boxes in row and give them a id because i want to give the access using these check boxes so i'm stuck there how i get the value from the form and save that value in the data base.

Front End

https://i.imgur.com/kY6jsNL.png

I have 5 tables 1: Users

    Schema::create('users', function (Blueprint $table) {
        $table->increments('id');
        $table->unsignedInteger('role_id');
        $table->string('name');
        $table->string('email')->unique();
        $table->string('password');
        $table->string('status');
        $table->string('image');
        $table->rememberToken();
        $table->timestamps();
        $table->foreign('role_id')->references('id')->on('roles')->onDelete('cascade');

    });

2: Roles

     Schema::create('roles', function (Blueprint $table) {
        $table->increments('id');
        $table->string('name');
        $table->string('permission')->nullable();
        $table->timestamps();
    });

3: Module

    Schema::create('modules', function (Blueprint $table) {
        $table->bigIncrements('id');
        $table->string('name');
        $table->timestamps();
    });

4: Access Name

    Schema::create('access_names', function (Blueprint $table) {
        $table->bigIncrements('id');
        $table->string('name');
        $table->timestamps();
    });

5: Module Permissions

    Schema::create('module_permissions', function (Blueprint $table) {
        $table->bigIncrements('id');
        $table->unsignedInteger('user_id');
        $table->unsignedInteger('role_id');
        $table->unsignedBigInteger('access_id');
        $table->unsignedBigInteger('module_id');
        $table->enum('status', ['0', '1']);
        $table->timestamps();
        $table->foreign('user_id')
            ->references('id')->on('users')
            ->onDelete('cascade');
        $table->foreign('role_id')
            ->references('id')->on('roles')
            ->onDelete('cascade');
        $table->foreign('access_id')
            ->references('id')->on('access_names')
            ->onDelete('cascade');
        $table->foreign('module_id')
            ->references('id')->on('modules')
            ->onDelete('cascade');

    });

These are the tables I want to store the data in the 'Module Permissions' so i am unable to save data in this DB because i am getting data in controller as an array and how i save data of the form. here are some detail which is my Module and which is My Access name These are fixed they never increase

https://i.imgur.com/pG2NDVf.png

This is the form where form i am sending data to the controller that name is

Permission.blade.php

    @foreach($roles as $role)
<form class="form-horizontal module-permission" name="name_" action="" method="post">
    <br/>
    @php
        $user_id = DB::table('users')->where('role_id',$role->id)->pluck('id');

    @endphp
    @csrf
    <div class="panel panel-default">
        <div class="panel-heading">
            <h4 class="panel-title">
                <a class="accordion-toggle collapsed" data-toggle="collapse" data-parent="#accordion" href="#collapse_"><i class="fa fa-bars"></i>  Permission For :  </a>
            </h4>
        </div>
        <div id="collapse_" class="panel-collapse collapse ">
            <div class="panel-body">
                <table class="table table-bordered dt-responsive rolesPermissionTable">
                    <thead>
                    <tr class="showRolesPermission">
                        <th scope="col">Modules</th>
                        <th scope="col">Create</th>
                        <th scope="col">Read</th>
                        <th scope="col">Update</th>
                        <th scope="col">Delete</th>
                    </tr>
                    </thead>
                    <tbody>
                    <input type="hidden" name="data['Role'][]" value="" />
                    <input type="hidden" name="data['User']" value= />
                    @foreach($module_permissions as $module_permission)
                        @if($module_permission->id == 1)
                            <tr>
                                <th scope="col" colspan="5" class="showRolesPermission text-center">
                                    <input type="hidden" name="data['Expense'][1]" value="1" />Expenses
                                </th>
                            </tr>
                        @endif
                        @if($module_permission->id == 2)
                            <tr>
                                <th scope="col" colspan="5" class="showRolesPermission text-center">
                                    <input type="hidden" name="data['ExpenseCategory'][2]" value="2" />Expenses Category
                                </th>
                            </tr>
                        @endif
                        @if($module_permission->id == 3)
                            <tr>
                                <th scope="col" colspan="5" class="showRolesPermission text-center">
                                    <input type="hidden" name="data['Income'][3]" value="3" />Incomes
                                </th>
                            </tr>
                        @endif
                        @if($module_permission->id == 4)
                            <tr>
                                <th scope="col" colspan="5" class="showRolesPermission text-center">
                                    <input type="hidden" name="data['IncomesCategory'][4]" value="4" />Incomes Category
                                </th>
                            </tr>
                        @endif
                            <tr>
                                <th scope="row" class="thfont">Own Entries</th>
                                @foreach($access_names as $access_name)
                                <td><input type="checkbox" name="data[" value="1" /></td>
                                @endforeach
                            </tr>
                        <tr>
                            <th scope="row" class="thfont">All Entries</th>
                            <td>-</td>
                            @foreach($access_names as $access_name)
                                @if($access_name->id != 1)
                                    <td><input type="checkbox" name="data[" value="1" /></td>
                                @endif
                            @endforeach
                        </tr>
                    @endforeach
                    </tbody>
                </table>
            </div>
        </div>
    </div>
    <hr>
    <input type="submit" name="save" value="Save Permission" class="btn btn-wide btn-success margin-top-20" />
</form>@endforeach

And this is the my script code where i am send this data to the controller.

    $('.module-permission').submit(function (event) {
                var form = $('form')[0]; // You need to use standard javascript object here
                var formData = new FormData(form);
                $.ajax({
                    url: "",
                    data: formData,
                    type: 'POST',
                    contentType: false, // NEEDED, DON'T OMIT THIS (requires jQuery 1.6+)
                    processData: false, // NEEDED, DON'T OMIT THIS
                    success: function () {// ... Other options like success and etc

                    }
                });
            });

This is my Controller What should i write in controller that i am saved that data in the Module Permission table.

ModulePermissionController.php

    public function store(Request $request)
{

    $module_permission = new ModulePermission();
   $data1 = $request->data;
    dd($data1);
}

And I am getting data in the controller like this..

https://i.imgur.com/xvWfCJ7.png

I am get the data of Own Entries But not getting the data of All entries i give the same name to the both of the OWN and ALL which is name = "Data[Here is the permission id][ and here is the access name] and the value which is "1";

Like this

https://i.imgur.com/Sqi5SA4.png

and now My First Question is this how i am getting the both data that is own entries and all entries and how i am adjusting these data in the controller so that i insert these data into the data base easily.. And after insertion i am easily fetch data from the database. Because i want to give the permission to the user from this check boxes that are display in the picture. Advance Thanks!




Aucun commentaire:

Enregistrer un commentaire